问题描述
有人可以告诉我这里的最大区别是什么,为什么后者不起作用?
Can anyone tell me what the big difference here is and why the latter doesn't work?
test="ls -l"
两者现在都可以正常工作:
Both now work fine:
eval $test
echo `$test`
但是在这种情况下:
test="ls -l >> test.log"
eval $test
echo `$test`
后者将不起作用.这是为什么?我知道eval只是在执行脚本,而撇号正在执行脚本并将结果作为字符串返回.是什么使得无法在命令中使用>>
或类似内容来执行?也许有一种方法可以使它与撇号一起使用,但我做错了什么?
The latter will not work. Why is that? I know that eval is just executing a script while the apostrophes are executing it and return the result as a string. What makes it not possible to use >>
or simmilar stuff inside the command to execute? Maybe is there a way to make it work with apostrophes and I'm doing something wrong?
推荐答案
在使用反引号执行命令时,发送到shell的命令是:
When you're using backticks to execute your command, the command being sent to the shell is:
ls -l '>>' test.log
使得ls
的>>
和test.log
自变量(请注意>>
周围的引号).
which makes both >>
and test.log
arguments to ls
(note the quotes around >>
).
使用eval
时,正在执行的命令是:
While using eval
, the command being executed is:
ls -l >> test.log
(通过说出bash -vx scriptname
来执行脚本,以查看发生了什么.)
(Execute your script by saying bash -vx scriptname
to see what's happening.)
这篇关于评估号和反引号之间的差异(反撇号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!