On a Unix or Linux system, the eval
command is used to run the arguments as a shell command.
When you have a Unix or Linux command saved in a variable and wish to run that command, the eval
command is useful. The command evaluates the argument first, then executes the command it contains.
eval [arg]...
The arguments are read and combined into a single command. The shell then reads and executes this command, and the result is returned as the value of eval
.
The command below executes the echo
command and prints hi
.
eval echo hi
The command below executes the ls
command stored in the variable COMM
.
Refer to What is the ls command in Linux? to learn more about
ls
command.
COMM="ls"eval $COMM