95

What is the correct way to call some command stored in variable?

Are there any differences between 1 and 2?

#!/bin/sh
cmd="ls -la $APPROOTDIR | grep exception"

#1
$cmd

#2
eval "$cmd"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Volodymyr Bezuglyy
  • 16,295
  • 33
  • 103
  • 133

4 Answers4

107

Unix shells operate a series of transformations on each line of input before executing them. For most shells it looks something like this (taken from the Bash man page):

  • initial word splitting
  • brace expansion
  • tilde expansion
  • parameter, variable and arithmetic expansion
  • command substitution
  • secondary word splitting
  • path expansion (aka globbing)
  • quote removal

Using $cmd directly gets it replaced by your command during the parameter expansion phase, and it then undergoes all following transformations.

Using eval "$cmd" does nothing until the quote removal phase, where $cmd is returned as is, and passed as a parameter to eval, whose function is to run the whole chain again before executing.

So basically, they're the same in most cases and differ when your command makes use of the transformation steps up to parameter expansion. For example, using brace expansion:

$ cmd="echo foo{bar,baz}"

$ $cmd
foo{bar,baz}

$ eval "$cmd"
foobar foobaz
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JB.
  • 40,344
  • 12
  • 79
  • 106
  • 6
    How to do `eval "$cmd"` without writing `eval`? `$($cmd)`? `${$cmd}`? – Steven Lu May 08 '13 at 21:01
  • 2
    @StevenLu, none of those are equivalent -- intentionally so: An `eval` operation parses data as syntax; it's thus very security-sensitive, and doing it implicitly would be very bad form. – Charles Duffy Jan 26 '17 at 19:19
8

If you just do eval $cmd when we do cmd="ls -l" (interactively and in a script), you get the desired result. In your case, you have a pipe with a grep without a pattern, so the grep part will fail with an error message. Just $cmd will generate a "command not found" (or some such) message.

So try use to eval (near "The args are read and concatenated together") and use a finished command, not one that generates an error message.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Henno Brandsma
  • 2,116
  • 11
  • 12
0

$cmd would just replace the variable with it's value to be executed on command line. eval "$cmd" does variable expansion & command substitution before executing the resulting value on command line

The 2nd method is helpful when you wanna run commands that aren't flexible eg.
for i in {$a..$b}
format loop won't work because it doesn't allow variables.
In this case, a pipe to bash or eval is a workaround.

Tested on Mac OSX 10.6.8, Bash 3.2.48

Zimba
  • 2,854
  • 18
  • 26
-4

I think you should put

`

(backtick) symbols around your variable.

roschach
  • 8,390
  • 14
  • 74
  • 124
Nickolodeon
  • 2,848
  • 3
  • 23
  • 38
  • 5
    This executes the output of the command, which e.g. in the case of ls -l will generate a message like "total" command not found" (because total ... is part of the output of ls -l, e.g.) So this is NOT what you want. – Henno Brandsma Jan 12 '11 at 12:28