eval - parse and execute (evaluate) a command string
eval string ...
The eval(1) command is a special shell built-in command that concatenates its arguments, separating them with spaces, and then parses the resulting command line as if it were a new command. The eval(1) command is useful for making a second pass over a command string if it contains shell variables that, in turn, contain:
The eval(1) command is particularly useful in shell scripts that must build up a command line that is then evaluated and executed.
Because a command passed to eval(1) goes through shell variable and redirection expansion twice, you must use care in trying to pass literal metacharacters.
The eval(1) command gives the following diagnostics:
Otherwise, eval(1) returns the exit status of the command that eval(1) executes.
The following shell script shows how to include a redirection character in a variable:
while getopts f:t: opt
do
case "$opt" in
'f') FILE="> $OPTARG"
;;
't') TEE="| tee $OPTARG"
;;
esac
done
shift $OPTIND-1
eval awk -f script.awk $@ $TEE $FILE
Without eval(1), the command line would not work if any options were given. Because redirection is resolved before the variables are expanded, the FILE argument (for example) would be expanded to a file named "> $OPTARG" (space included), which doesn't exist.
If the command line contains redirection characters that are not in a shell variable, you may need to quote it.
ksh(1)
sh(1)