2008年3月18日 星期二

Bash Command substitution & Subshell - 080320

Command substitution
$(...) or `...`

Subshell
(...)

Nested command substitution
ls -al $(echo $(ls $(pwd)))


the following is ok...

C2QSERVER lsdk # echo $(a=b;echo $a)=c;echo $b
b=c
but the following failed...
C2QSERVER lsdk # $(a=b;echo $a)=c;echo $b
bash: b=c: command not found

C2QSERVER quicksec # $($(a=b;echo $a)=c);echo $b
bash: b=c: command not found
while the following will work..
C2QSERVER quicksec # eval $(a=b;echo $a)=c;echo $b
c

why?
bash shell只有認是直接的 a=b 這種語法是變數設定
所以 a 是變數可以變動的話,請愛用 eval 代為帶入展開即可
eval $(a=b;echo $a)=c;echo $b
a=b; b=c; echo $b=$(eval echo \$$a)

h

沒有留言: