Eval syntax
Eval arg1 arg2 ...
The function of Eval is to take the following parameters Arg1 Arg2 and so on as a pipeline, and then re-execute the shell process pipeline (about pipeline, see http://www.cnblogs.com/chaoguo1234 /p/5724321.html)
Suppose you have the following script statement:
var="ls | more"$var
The above program is intended to enumerate the files of the current directory, and then use more to show the results, but the actual execution will be error:
Ls:can ' t access |:no Such file or directory
Ls:can ' t access more:no Such file or directory
The reason is that after the command to replace the $var, the shell for Command lookup, at this time to find the command is LS, followed by | And more as the parameters of LS, and the current directory is not named | and more files and directories, so the return error. Take a look at the shell's process of processing the command to clear:
As you can see, the shell performs a command lookup when the $var variable extension is executed.
The way to solve this is to use the eval command:
var="ls | more"eval $var
After the variable substitution of $var is done, it becomes the eval ls | More, and then, according to the syntax of Eval, the shell would have LS | More as a pipeline, start from the first step.
Eval in the shell