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?
eval is 'expression value' i.e.
is execute in same way in terminal as
whether
echo is for display purpose only.
When you're using backticks to execute your command, the command being sent to the shell is:
which makes both
>>
andtest.log
arguments tols
(note the quotes around>>
).While using
eval
, the command being executed is:(Execute your script by saying
bash -vx scriptname
to see what's happening.)