Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

eval - construct command by concatenating arguments

&pagelevel(4)&pagelevel

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 ...]

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

The following command line cannot be executed as intended unless eval is used:


$ cmd='date;echo name'

$ $cmd

sh:date;echo: not found

$ eval $cmd

Mon Mar 09 15:46:02 MEZ 2009

name


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.
On executing this shell script, you will receive the following output on the screen (without the line numbers):


$ sh evaltest

1  + loginname=max

2  + eval group1=max1
3  + group1=max1

4  + eval echo $group1

5  + echo max1
6  max1

:


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:
Here the shell has substituted the value 1 for $i and the value max for $loginname. eval must be invoked as the shell does not recognize the equals sign as an assignment operator the first time the loop is run. The shell executes an assignment only if the variable name (i.e. the string in front of the equals sign =) begins with a letter or the underscore character _ and contains letters, numbers and underscores only. group$i is an illegal variable name as it contains the dollar character.

Line 3 shows that the assignment is made.
When the command-line arguments are interpreted by the shell a second time, the equals sign is interpreted as intended, since the argument preceding the = sign represents a legal name for a shell variable.

Line 4 demonstrates how the second eval command line is processed:
the shell has substituted the value 1 for the positional parameter $i and removed the escape character \ preceding the $. eval is required in this case to ensure that the shell replaces the group1 variable by its value.

Line 5 shows how the echo command is processed:
Here, the shell has substituted the value max1 for $group1. Line 6 contains the output of the echo command.

See also

set