更改扩展顺序(bash)

命令replace后我需要参数扩展。

GNU bash,版本4.1.5(1)

$ foo() { echo \$a; } $ a=5 $ echo $(foo) $a 

可能吗?

testing:

 #!/bin/bash echo $a 

跑:

 a=5 echo $(./test) 

如果testing:

 #!/bin/bash echo \$a 

跑:

 echo $(./test) $a 

不要工作(

无需更改源代码即可更改订单。

你可以使用eval:

 eval echo $(foo) 

男子bash:

  The order of expansions is: brace expansion, tilde expansion, parame‐ ter, variable and arithmetic expansion and command substitution (done in a left-to-right fashion), word splitting, and pathname expansion. 

… 什么?

 $ foo() { echo $a; } $ a=42 $ echo $(foo) 42 
 echo `eval foo` 

这是你想要的吗?

function:

 $ a=5 $ foo() { echo \$a; } $ foo $a $ eval echo $(foo) 5 

脚本:

 #!/bin/bash echo $a 

跑:

 $ a=5 $ ./test $ export a $ ./test 5 

新脚本:

 #!/bin/bash echo 'echo $a' 

跑:

 $ a=5 $ ./test echo $a $ eval $(./test) 5 

Python示例:

 $ a=5 $ python -c 'print "echo $a"' echo $a $ eval $(python -c 'print "echo $a"') 

另一个Python例子:

 $ a=5 $ python -c 'print "a=42"' a=42 $ echo $a 5 $ declare $(python -c 'print "a=42"') $ echo $a 42