Unix shells perform a series of conversions on each line of input before executing them. For most shells, it looks something like this (taken from the bash manpage):
- splitting of the initial word
- bracket extension
- tilde expansion
- parameter, variable and arithmetic expansion
- command substitution
- word splitting
- extension of the path (aka globe)
- delete quote
Using $cmd directly replaces it with your command during the parameter expansion phase, and then undergoes all of the following conversions.
Using eval "$cmd" does nothing until the delete phase of the quote, where $cmd returned as is, and passed as a parameter to eval , whose function is to start the whole chain again before executing.
Basically, they are mostly the same and different when your team uses the conversion steps before expanding the parameters. For example, using a parenthesis extension:
$ cmd="echo foo{bar,baz}" $ $cmd foo{bar,baz} $ eval "$cmd" foobar foobaz
Jb. Jan 12 '11 at 12:29 2011-01-12 12:29
source share