The POSIX shell built-in eval can be used to pass command-line arguments to the sh shell for execution. The shell evaluates these arguments and then executes them as commands.
The specified command-line arguments are thus evaluated twice by the shell:
First, when the shell processes the eval command line.
Second, when the processed command-line arguments are executed by the shell as commands. Each command-line is processed by the shell before execution.
When do you need eval?
The shell processes each command line in a number of steps (see section “Execution”), interpreting specific metacharacters in each processing step. The original command line may be modified by the individual processing steps.
If, for example, the shell replaces a variable with its value or a command with its output, metacharacters may occur in the command line which may not be interpreted by the shell in subsequent processing steps. The shell built-in eval makes sure that metacharacters left by the previous step are interpreted in the following step (see also Examples).
Syntax
eval [argument ...] |
Any string delimited by blanks or tabs. The last argument must be terminated by a command separator. You may specify any number of arguments with at least one blank or tab between them. The specified arguments are executed by the shell as a command. |
Locale
The following environment variables affect the execution of eval: LANG Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined. LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other. LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES. |
Example 1
|
To begin with, the shell replaces the cmd variable with its value. After the value has been substituted for the variable, the shell no longer recognizes the colon as a command separator. Calling eval causes the shell to interpret the semicolon as intended, since the $cmd argument is processed twice, i.e. rescanned by the shell.
Example 2
The shell script evaltest will be used to demonstrate how the eval command is processed by the shell. The script file has the following contents: set -x loginname=max for i in 1 2 3 4 do eval group$i=$loginname$i eval echo \$group$i done set -x causes the shell running the shell script to write the commands processed to standard error output before executing them.
This example shows only what is output the first time the loop is run. Line 2 shows how the first eval command line is processed: Line 3 shows that the assignment is made. Line 4 demonstrates how the second eval command line is processed: Line 5 shows how the echo command is processed: |
See also
set |