Linux Command Reference ยท Shell Builtins
Linux eval command
Reference for the Bash eval builtin: build and run a command from a string, understand double expansion, and avoid injection with printf %q and namerefs.
eval joins its arguments into one string, then runs that string through the whole shell parser a second time and executes the result in the current shell. The first parse happens when you write the command; the second is the one eval adds. Anything that can appear in a script can appear in that string: variables, redirections, pipelines, function definitions, assignments.
That power is exactly why eval has a bad reputation. If any part of the string comes from outside the script, it can inject commands. Most uses people reach for have a safer replacement, such as arrays, declare -n namerefs or printf -v. Still, eval is the right tool in a few places, and you will meet it in other people's scripts (eval "$(ssh-agent)").
cmd='echo hello world' eval "$cmd"
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
eval [ARGUMENT ...]
| Name | Description |
|---|---|
ARGUMENT |
Concatenated with single spaces into one string, then parsed and executed as shell code. |
Returns: the exit status of the executed command. If there are no arguments or they are all empty, the status is 0. A syntax error in the string gives status 2 (the exact behavior in a non-interactive shell can end the script).
Usage notes
- Quotes protect twice. Because the string is parsed again, you often need
\$, nested quotes orprintf %q. Test withechoin place of the real command first. - Same shell. The code runs in the current shell, so assignments and
cdpersist, unlikebash -c, which starts a child. - Injection risk. Never
evaltext that includes user input, file names, or network data without quoting it withprintf '%q'. A file named$(rm -rf ~)is a valid file name. - Prefer alternatives. Indirect expansion
${!name}, namerefs (declare -n), arrays for argument lists, andprintf -vcover most cases that once neededeval. - Accepted uses.
eval "$(ssh-agent -s)",eval "$(dircolors -b)"and other tools that print shell assignments are meant to be evaluated. Run such commands only from sources you trust. - Arguments are joined.
eval a bis the same aseval "a b"; word boundaries from the original arguments are lost. - Performance. Each call re-parses the string, which is slow in tight loops.
- Debugging.
set -xshows the final string eval executes, which makes quoting mistakes visible.
Related
printf -v,declare,source,exec, Namerefs- Lessons: Expansion order, Quoting, Script debugging