12.11. The eval Command and Parsing the Command Line

The eval command evaluates a command line, performs all shell substitutions, and then executes the command line. It is used when normal parsing of the command line is not flexible enough for what we want to do.

Example 12.76.
1   $ set a b c d
2   $ echo The last argument is \$$#
3   The last argument is $4

4   $ eval echo The last argument is \$$#
					The last argument is d

5   $ set -x
    $ eval echo The last argument is \$$#
					+ eval echo the last argument is '$4'
					++ echo the last argument is d
					The last argument is d
				

EXPLANATION

  1. Four positional parameters are set.

  2. The desired result is to print the value of the last positional parameter. The \$ will print a literal dollar sign. The $# evaluates to 4, the ...

Get UNIX® Shells by Example, Third Edition 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.