Skip to content
Tutorials Center
My learning

Topics

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)").

Terminal
cmd='echo hello world'
eval "$cmd"

Try it

Bash Double expansion

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.

Bash Why eval is dangerous, and safer habits

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

Terminal
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 or printf %q. Test with echo in place of the real command first.
  • Same shell. The code runs in the current shell, so assignments and cd persist, unlike bash -c, which starts a child.
  • Injection risk. Never eval text that includes user input, file names, or network data without quoting it with printf '%q'. A file named $(rm -rf ~) is a valid file name.
  • Prefer alternatives. Indirect expansion ${!name}, namerefs (declare -n), arrays for argument lists, and printf -v cover most cases that once needed eval.
  • 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 b is the same as eval "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 -x shows the final string eval executes, which makes quoting mistakes visible.