Difference between eval and backticks (reverse apostrophe)

耗尽温柔 提交于 2019-12-12 08:15:39

问题


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`

But in this case:

test="ls -l >> test.log"

eval $test
echo `$test`

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?


回答1:


When you're using backticks to execute your command, the command being sent to the shell is:

ls -l '>>' test.log

which makes both >> and test.log arguments to ls (note the quotes around >>).

While using eval, the command being executed is:

ls -l >> test.log

(Execute your script by saying bash -vx scriptname to see what's happening.)




回答2:


eval is 'expression value' i.e.

test="ls -l >> test.log"
eval $test

is execute in same way in terminal as

ls -l >> test.log

whether

echo is for display purpose only.



来源:https://stackoverflow.com/questions/18849567/difference-between-eval-and-backticks-reverse-apostrophe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!