The ‘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:

$ set -- one two three  # Sets $1 $2 $3
$ echo $1
one

$ n=1
$ echo ${$n}       ## First attempt to echo $1 using brackets fails
bash: ${$n}: bad substitution

$ echo $($n)       ## Second attempt to echo $1 using parentheses fails
bash: 1: command not found

$ 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?

11 Answers
11

Leave a Comment