8

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?

MrE
  • 19,584
  • 12
  • 87
  • 105
Kelu Thatsall
  • 2,494
  • 1
  • 22
  • 50
  • 2
    they are called `backticks`. – dogbane Sep 17 '13 at 12:22
  • @dogbane, oh that was a part of the question also :D Thanks, didn't know how they were called. – Kelu Thatsall Sep 17 '13 at 12:29
  • To answer your last question: Yes, you are doing something wrong. If you are using eval or putting code to be executed in strings you are doing something wrong 99.9% of the time. Your script will be harder to reason about, harder to debug, and there are security issues: http://mywiki.wooledge.org/BashFAQ/048 – aktivb Sep 17 '13 at 12:43

2 Answers2

6

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.)

devnull
  • 118,548
  • 33
  • 236
  • 227
  • Well I kinda got it out of the error info, but why is that happening? Is there a way to prevent it with backticks? – Kelu Thatsall Sep 17 '13 at 12:36
  • @KeluThatsall In the first case, everything including the redirection `>>` is a part of a string and is interpreted literally. – devnull Sep 17 '13 at 12:38
  • 2
    Does it mean, that backticks are actually splitting the string they are interpreting with spaces and take the first element as a command, and the later as parameters? And you just cannot do anything about that? – Kelu Thatsall Sep 17 '13 at 12:42
  • 1
    Yes, you can't do *anything* about it. Either use `eval` or something like: `test="ls -l"; $(test) >> "test.log"`. – devnull Sep 17 '13 at 12:44
  • 1
    If string is split up like that, then why isn't it when doing just \`ls -l >> test.log\` - these backticks work and produce a file! why isn't \`$something\` <=> \`contentOfSomething\` ? – JohnyTex Jun 29 '20 at 12:34
  • @JohnyTex I think it is $ does the var expanding and wordspliting and that 's why we usually put double quotes around $. https://stackoverflow.com/questions/12789079/bash-eval-replacement-not-always-equivalent https://mywiki.wooledge.org/WordSplitting – Stan Mar 20 '22 at 00:14
0

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.

Roshan
  • 1,459
  • 1
  • 14
  • 20