The eval Command

The eval command reads and executes commands using the following format:

					eval
					commands
				

It causes commands to be expanded twice. For simple commands, there is no difference in execution with or without eval:

					$ eval print ∼
					/home/anatole
				

Here is a more complicated command that illustrates the use of eval. We want the contents of the stext file redirected when variable X is accessed.

					$ cat stext
					She sells seashells by the seashore.
				

First, variable X is set to "<stext":

					$ X="<stext"
				

When the value of X is printed using simple variable expansion, it generates an error:

					$ cat $X
					<stext: No such file or directory
				

Using the eval command, X is first expanded to <stext, then the command cat <stext is executed. This causes the ...

Get Korn Shell: Unix and Linux Programming Manual, Third Edition, The now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.