eval(1) eval(1)
NAME
eval - evaluate command-line arguments and execute as command
SYNOPSIS
eval argument ...
DESCRIPTION
The shell built-in eval can be used to pass command-line arguments to
the sh or ksh 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 sh(1),
COMMAND LINE PROCESSING), 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).
OPERANDS
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 LCMESSAGES environment variable governs the language in which
message texts are displayed. If LCMESSAGES is undefined or is defined
as the null string, it defaults to the value of LANG. If LANG is like-
wise undefined or null, the system acts as if it were not internation-
alized.
Page 1 Reliant UNIX 5.44 Printed 11/98
eval(1) eval(1)
EXAMPLES
Example 1
The following command line cannot be executed as intended unless eval
is used:
$ cmd='date;who'
$ $cmd
date;who: not found
$ eval $cmd
Thu Mar 09 15:46:02 MET 1989
udo tty04 Mar 9 09:20
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.
Page 2 Reliant UNIX 5.44 Printed 11/98
eval(1) eval(1)
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.
NOTES
Some differences in behavior may occur when using eval, depending on
which shell is being used. The possible differences are not described
specifically.
SEE ALSO
ksh(1), set(1), sh(1).
Page 3 Reliant UNIX 5.44 Printed 11/98