Previous Table of Contents Next

Module 42


The internal eval command interprets and expands a command line before the shell interprets and expands the line. This allows you to:

*  Execute a command line you read using the read command.
*  Find the value of a variable whose name is derived from the value of another variable.
*  To repeat a stage or portion of command processing.
*  To apply results of expansions to earlier stages of the expansion.


Following is the general format of the eval command.

     eval [ command arg ... ]


The following list describes the arguments that may be passed to the eval command.

command The name of any valid command (internal or external).
arg The arguments used by the specified command. The args are separated by a space.


The shell expands arguments to eval using standard command processing rules. Then the shell forms a space-separated string of all the arguments. The shell reads the string as a command line and processes it again and executes it.

You can use eval to execute a command you read using the read command. For example,

     cj> read CMD
     cat /etc/group | grep mylogin
     cj> eval "$CMD"

processes the CMD variable. If you enter any special characters, such as ; or |, eval expands them and the shell reprocesses the results before executing the command line. If you did not use the eval command, the CMD variable would not be interpreted correctly and certain commands would not run. Another example illustrates this concept.

     cj> CMD='date | wc'
     cj> $CMD
     usage: date [-a sss.fff] [-u] [tformat] [mmddhhmm[yy]]
     cj> eval $CMD
     Sat Jan 5 22:54:04 CST 1989

The first line sets CMD to a command containing a pipe. The second command trys to execute the variable CMD. The execution fails because the pipe is not expanded and is passed to date as an argument. Thus date complains about a bad argument. The third line expands CMD to date | wc. The shell executes the line as a proper command.

The following example illustrates how you can use eval to expand variables within a command line to be the name of another variable.

     cj> eval last='$'{$#}    # expands to last positional parameter
     cj> X=10
     cj> Y=X
     cj> echo '$'$Y
     cj> eval echo '$'$Y


The eval command provides an extended feature allowing you to do special command processing. It is used to evaluate variables twice, so you can use the value of one variable as a variable name. The other popular use of eval is to process an input line as a command. Without eval this would be next to impossible.


In this activity you use the eval command to expand the positional parameters to check for a file name being specified in the last parameter. Begin at the shell prompt.

1.  Type the following lines of code; press Return at the end of each line.
     cj> set One Two Three Four
     cj> if eval [ ! -f \${$#} ]
     then echo "$4: last argument must be a file!"
        #exit 1
C Shell
There is no reason for eval to perform this function in the csh since the $#argv option is available. So we'll show you a different use for eval also useful in sh and ksh. This use of eval is often seen in shell scripts.
        cj> set input=$<
        cj> echo $input
        cj> eval echo $input

This code checks for the last argument on the command line to be the name of a file that exists. The first line sets the positional parameters to One Two Three Four. Since Four is not the name of a file, your code will return the message about the last file. The exit is commented out so you are not logged off when the command executes.

2.  Turn to Module 44 to continue the learning sequence.

Previous Table of Contents Next