Bash eval builtin command

Updated: 03/13/2021 by Computer Hope
eval command

On Unix-like operating systems, eval is a builtin command of the Bash shell. It concatenates its arguments into a single string, joining the arguments with spaces, then executes that string as a bash command. It's similar to running bash -c "string", but eval executes the command in the current shell environment rather than creating a child shell process.


eval evaluates all of its string arguments, concatenates them with a space, then evaluates that string and runs it as a command.

The strings provided to eval may contain reserved words. For example, they may contain loop keywords, such as, or conditionals, such as if..then..elif. These are evaluated in a first pass, then the resulting string is then evaluated.


eval [argument ...]


c="echo"; a1="Hello, "; a2="World!"; eval $c $a1 $a2

Assign strings to variables c, a1, and a2. Then, use eval to evaluate those arguments and join them into a single string, with a space between each. Then, run that string as a command, "echo Hello, World!". Output:

Hello, World!
cmd1="cmd2"; cmd2="echo Hi!"; eval \${$cmd1}

Here, eval is used to provide an additional layer of evaluation before a command is executed. Specifically, eval evaluates \${$cmd1} to "${cmd2}" (the backslash escapes the dollar sign, so that it evaluates as a literal $ character), then passes that string to bash for execution. The command ${cmd2} is evaluated by bash using parameter expansion (see parameter expansion in bash for more information). The end result is the command "echo Hi!". Output:


Example: eval $(ssh-agent)

The ssh-agent helper software stores SSH keys and passwords in memory, and automatically uses them to authenticate new SSH connections without user input. Its output is designed to be evaluated with eval.

eval $(ssh-agent)

This is the standard way to start ssh-agent. The ssh-agent command is enclosed in the command substitution form, $( .. ). This form executes the enclosed command string, and uses the output as an argument in the current command.

If instead you were to run ssh-agent normally, it would execute in a subshell and output its shell configuration:

SSH_AUTH_SOCK=/var/folders/4r/ysjs10bh4cqhxpwm80000gn/T//ssh-IAMlMcDf/agent.5101; export SSH_AUTH_SOCK;
echo Agent pid 5102;

This alone isn't useful, though - the subshell cannot affect the environment of the parent shell. So, this configuration is not known by the current terminal session that executed the command - or any new sessions you create. When you exit the shell, the configuration is forgotten. If you try to initiate a new SSH connection, you are always prompted for your SSH key password, because the current shell is not aware of the agent configuration.

However, if you use eval $(ssh-agent), the configuration is added to the environment of the current shell. When you initiate an SSH connection, the agent prompts you for the key password once, and configures the ssh-agent daemon process. Then, when you create new SSH connections, the daemon automatically authenticates you with the credentials stored in memory. It remembers the credentials until you log out of the OS, or restart the computer.

The next time you want to start the daemon, run eval $(ssh-agent) again, and enter your password.

exec — Destroy the current shell and replace it with a new process.