7.9 Here Documents


7.9 Here Documents

Sometimes you want to print a large section of text or feed a lot of text to another command. Rather than using several echo commands, you can employ the shell's here document feature.

The following is a script that shows how here documents work:

 #!/bin/sh DATE=`date` cat  <<EOF  Date: $DATE The output above is from Unix date command. It's not a very interesting command.  EOF  

The items in bold control the here document. <<EOF tells the shell to redirect all lines that follow to the standard input of the command that precedes <<EOF , which in this case is cat . The redirection stops as soon as the EOF marker occurs on a line by itself. The marker that you use doesn't have to be EOF ” use any string that you like, but remember that you must use the same marker at the beginning and end of the here document, and that convention dictates that the marker be in all uppercase letters .

Notice also that there is a shell variable in the here document. The shell expands shell variables inside here documents. This is especially useful when you're printing out some kind of report and have many variables to put into a larger form.




How Linux Works
How Linux Works: What Every Superuser Should Know
ISBN: 1593270356
EAN: 2147483647
Year: 2004
Pages: 189
Authors: Brian Ward

Similar book on Amazon

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