EVAL(1SH) COMMAND REFERENCE EVAL(1SH)
NAME
eval - execute arguments (sh built-in)
SYNOPSIS
eval [ args . . . ]
DESCRIPTION
eval 's arguments are read as input to the shell and the
resulting command(s) are executed. This command is useful
for executing the output of commands and the contents of
shell variables.
EXAMPLES
This example shows a shell script, called pathdir, which
prints the word 'cd' followed by the name of the directory
containing the name of the given executable file.
#!/bin/sh
if test $# -ne 1
then
echo "$0 : usage : "`basename $0`" file" 1>&2
exit 1
fi
where=`pathof $1`
if test $? -ne 0
then
exit 1
fi
echo -n "cd "
expr "$where" : '^\(.*)/.*$'
Executing the command eval pathdir sh will effect the
command cd /bin, since that is where the program sh resides.
For another example of using eval to execute the output from
a command, see the documentation for tset(1).
Another use of eval is to store commands in shell variables
and have them execute. For example, the commands:
w='who|sort'
eval $w
will cause the command 'who | sort' to be executed. Note
that simply typing '$w' on the command line will not work in
this case. The reason for this is that the shell has already
passed the stage of evaluating the '|' when it expands the
variable.
It is possible to have shell variables which execute
correctly without eval as in the following example:
Printed 4/6/89 1
EVAL(1SH) COMMAND REFERENCE EVAL(1SH)
w='eval who|sort'
$w
It is important to note that the commands are run in the
current shell, not a subshell. This means that the following
use of a shell variable works as expected:
bin='eval cd /bin;pwd'
$bin
The command $bin will print the line '/bin' and the current
shell becomes /bin.
RETURN VALUE
Eval returns the exit status of the last command executed.
SEE ALSO
break(1sh), cd(1sh), chdir(1sh), continue(1sh), csh(1csh),
echo(1sh), eval(1csh), exec(1sh), exit(1sh), export(1sh),
hash(1sh), login(1), pwd(1sh), read(1sh), readonly(1sh),
return(1sh), set(1sh), sh(1sh), shift(1sh), test(1sh),
times(1sh), trap(1sh), type(1sh), ulimit(1sh), umask(1sh),
unset(1sh), wait(1sh), which(1sh), and execve(2).
Printed 4/6/89 2
%%index%%
na:288,94;
sy:382,164;
de:546,392;
ex:938,1611;2909,399;
rv:3308,228;
se:3536,912;
%%index%%000000000116