2014-11-21 97 views
3

說我有一個變量:如何在變量中執行字符串命令?

T=6 

而且所謂的「行」,我從文件中讀取幷包含以下字符串/文本另一個變量:

echo "$T" 

現在我想執行寫在「線」命令,這樣我會得到:

6 

就像什麼,如果將只輸入命令我將獲得:

echo "$T" 

並獲得:

6 

我試着使用:

$line 

但我得到:

"$T" 

在此先感謝您的幫助。

+0

你變線應行='回聲 「$ T」'。然後你可以使用'echo $ line'。 – iqstatic 2014-11-21 11:24:37

回答

2

eval是你在找什麼

例如,考慮

$ T=6 
$ line='echo "$T"' 
$ eval $line 
6 

從手冊頁

eval [arg ...] 
       The args are read and concatenated together into a single command. This command is then read and executed by the shell, and its exit 
       status is returned as the value of eval. If there are no args, or only null arguments, eval returns 0. 
+1

[評估指揮和安全問題]的鏈接(http://mywiki.wooledge.org/BashFAQ/048)''eval「是一種常見的」邪惡「拼寫錯誤。 – 2014-11-21 14:04:13