Command Substitution


Earlier you saw how the shell substitutes the value of a variable into a command line. Command substitution is a similar feature that allows you to substitute the output of a command into your command line. To do this, you enclose the command in backquotes. Note that the backquote character (`) is different from the single quote character (’). On many keyboards, the backquote key is in the upper left, near the 1 key

Suppose the file names contains the e-mail addresses of the members of a working group:

 $ cat names rlf@library.edu nate@engineer.com liz@thebest.net

You can use command substitution to send mail to all of them by typing

 $ mail 'cat names'

When this command line is processed, the backquotes tell the shell to run cat with the file names as input, and substitute the output of this command (which in this case is a list of e-mail addresses) into the command line. The result is exactly the same as if you had entered the command

 $ mail rlf@library.edu nate@engineer.com liz@thebest.net

In the Korn shell and bash,

 $ mail $ (cat names)

works exactly the same way. It even makes sense-as with variables, the $ causes the shell to replace the command with its value. Because of this, and because the backquote character is so easily confused with single quotes, you may find this syntax preferable.




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net