Cover V09, I04x
Article
Listing 1
Listing 2
Sidebar 1

apr2000.tar


Sidebar 1: Here vs. There

If you want to input “here” in the same file as the shell script instead of over “there” in a temporary file, use the shell's “here document” facility. The here document temporarily redirects standard input, allowing multi-line input to be redirected. The syntax is “input operator <<” followed by a beginning and terminating “word”:

<<word

  .
  .
  .
word
All text, including new lines and special characters, from the beginning “word” to the terminating “word” is redirected to the script. A “word” may be any character or character string, but no spaces are allowed. If the terminating “word” is not on its own line, the here document doesn't terminate until the end of the script.

An example of mailing text to root:

mailx root << EOF
send some sample &*text*& and EOF
to user: root
EOF
The shell's special characters are treated as normal text just as they are with normal standard input. A here document can also remove all tab characters from the beginning of each line by using “<<-”. Not every UNIX variation supports “<<-”.