eval
Execute constructed commands or expressions
TLDR
Call echo with the "foo" argument
Set a variable in the current shell
SYNOPSIS
eval command_string [...]
PARAMETERS
command_string ...
One or more strings that are concatenated together to form the complete command to be evaluated and executed by the shell. These strings undergo all standard shell expansions (variable expansion, command substitution, filename expansion, etc.) in a second pass before the final command is run.
DESCRIPTION
The eval command is a shell built-in that reads its arguments, concatenates them into a single string, and then executes this resultant string as a new command in the current shell context. This provides an additional, crucial layer of expansion and interpretation, making it extremely powerful for constructing and executing dynamic commands at runtime. It's particularly useful when a command's arguments, options, or even its name are themselves stored within variables, necessitating an extra pass of shell processing (e.g., variable expansion, command substitution, filename expansion) before the command can be properly executed. While incredibly versatile and a core part of shell scripting, its usage demands careful consideration due to significant potential security implications when dealing with untrusted input.
CAVEATS
Security Risk: Using eval with untrusted input (e.g., user input, external file contents) can lead to severe security vulnerabilities, as it effectively allows arbitrary code execution. Always sanitize or validate input rigorously if eval is used.
Debugging Complexity: Commands executed via eval can be significantly harder to debug because the exact command that is run might not be immediately obvious from reading the script's source code, as it's generated dynamically.
Portability: While eval is a standard POSIX built-in, minor behavioral differences or subtle nuances might exist between various shell implementations (e.g., Bash, Zsh, Ksh).
DOUBLE EXPANSION PRINCIPLE
One of eval's primary uses is to force a second pass of shell expansion. This is particularly useful when you need to dereference a variable whose name is stored in another variable. Without eval, the shell would only expand the first level.
Example: VAR_NAME="MY_VAR"
MY_VAR="Hello World from Eval"
echo \$$VAR_NAME
(outputs: $MY_VAR)eval echo \$$VAR_NAME
(outputs: Hello World from Eval)
DYNAMIC COMMAND CONSTRUCTION
eval is invaluable when you need to construct a complete command string dynamically at runtime, where parts of the command (like options, arguments, or even the command name itself) are assembled from other variables or command substitutions. This allows for highly flexible script logic.
Example: cmd="ls -l"
target_dir="/tmp"
filter="*.txt"
eval \$cmd \$target_dir/\$filter
(executes: ls -l /tmp/*.txt)
HISTORY
The eval command has been a fundamental and indispensable part of Unix shells since their inception, notably present in the original Bourne shell (sh) from the 1970s. Its design allows for the dynamic construction and execution of commands, a feature crucial for flexible and powerful scripting in the early Unix environment. It has remained a core built-in across all modern POSIX-compliant shells due to its unique ability to provide an extra pass of shell interpretation, making it a cornerstone of advanced shell scripting.