Plug the Output of a Command into Another Command


$()

Command substitution takes the output of a command and plugs it in to another command as though you had typed that output in directly. Surround the initial command that's runthe one that's going to produce the output that's plugged inwith $(). An example makes this much clearer.

Let's say you just arrived home from a family dinner, connected your digital camera to your Linux box, pulled the new photos off of it, and now you want to put them in a folder named today's date.

$ pwd /home/scott/photos/family $  ls -1F 2005-11-01/ 2005-11-09/ 2005-11-15/ $ date "+%Y-%m-%d" 2005-11-24 $ mkdir $(date "+%Y-%m-%d") $  ls -1F 2005-11-01/ 2005-11-09/ 2005-11-15/ 2005-11-24/ 


In this example, date "+%Y-%m-%d" is run first, and then the output of that command, 2005-11-24, is used by mkdir as the name of the new directory. This is powerful stuff, and as you look at the shell scripts written by others (which you can easily find all over the Web) you'll find that command substitution is used all over the place.

Note

In the past, you were supposed to surround the initial command with backticks, the ` character at the upper left of your keyboard. Now, however, you're better advised to use the characters used in this section:$().




Linux Phrasebook
Linux Phrasebook
ISBN: 0672328380
EAN: 2147483647
Year: 2007
Pages: 288

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