eval command in Bash and its typical uses
After reading the bash man pages and with respect to this post.
I am still having trouble understanding what exactly the eval
command does and which would be its typical uses. For example if we do:
bash$ set -- one two three # sets $1 $2 $3
bash$ echo $1
one
bash$ n=1
bash$ echo ${$n} ## First attempt to echo $1 using brackets fails
bash: ${$n}: bad substitution
bash$ echo $($n) ## Second attempt to echo $1 using parentheses fails
bash: 1: command not found
bash$ eval echo \${$n} ## Third attempt to echo $1 using 'eval' succeeds
one
What exactly is happening here and how do the dollar sign and the backslash tie into the problem?
eval
takes a string as its argument, and evaluates it as if you'd typed that string on a command line. (If you pass several arguments, they are first joined with spaces between them.)
${$n}
is a syntax error in bash. Inside the braces, you can only have a variable name, with some possible prefix and suffixes, but you can't have arbitrary bash syntax and in particular you can't use variable expansion. There is a way of saying “the value of the variable whose name is in this variable”, though:
echo ${!n}
one
$(…)
runs the command specified inside the parentheses in a subshell (i.e. in a separate process that inherits all settings such as variable values from the current shell), and gathers its output. So echo $($n)
runs $n
as a shell command, and displays its output. Since $n
evaluates to 1
, $($n)
attempts to run the command 1
, which does not exist.
eval echo \${$n}
runs the parameters passed to eval
. After expansion, the parameters are echo
and ${1}
. So eval echo \${$n}
runs the command echo ${1}
.
Note that most of the time, you must use double quotes around variable substitutions and command substitutions (i.e. anytime there's a $
): "$foo", "$(foo)"
. Always put double quotes around variable and command substitutions, unless you know you need to leave them off. Without the double quotes, the shell performs field splitting (i.e. it splits value of the variable or the output from the command into separate words) and then treats each word as a wildcard pattern. For example:
$ ls
file1 file2 otherfile
$ set -- 'f* *'
$ echo "$1"
f* *
$ echo $1
file1 file2 file1 file2 otherfile
$ n=1
$ eval echo \${$n}
file1 file2 file1 file2 otherfile
$eval echo \"\${$n}\"
f* *
$ echo "${!n}"
f* *
eval
is not used very often. In some shells, the most common use is to obtain the value of a variable whose name is not known until runtime. In bash, this is not necessary thanks to the ${!VAR}
syntax. eval
is still useful when you need to construct a longer command containing operators, reserved words, etc.
Simply think of eval as "evaluating your expression one additional time before execution"
eval echo \${$n}
becomes echo $1
after the first round of evaluation. Three changes to notice:
- The
\$
became$
(The backslash is needed, otherwise it tries to evaluate${$n}
, which means a variable named{$n}
, which is not allowed) -
$n
was evaluated to1
- The
eval
disappeared
In the second round, it is basically echo $1
which can be directly executed.
So eval <some command>
will first evaluate <some command>
(by evaluate here I mean substitute variables, replace escaped characters with the correct ones etc.), and then run the resultant expression once again.
eval
is used when you want to dynamically create variables, or to read outputs from programs specifically designed to be read like this. See http://mywiki.wooledge.org/BashFAQ/048 for examples. The link also contains some typical ways in which eval
is used, and the risks associated with it.