[Next] [Previous] [Up] [Top] [Contents]

CHAPTER 9 Shell Programming

9.6 Here Document


A here document is a form of quoting that allows shell variables to be substituted. It's a special form of redirection that starts with <<WORD and ends with WORD as the only contents of a line. In the Bourne shell you can prevent shell substitution by escaping WORD by putting a \ in front of it on the redirection line, i.e. <<\WORD, but not on the ending line. To have the same effect the C shell expects the \ in front of WORD at both locations.

The following scripts illustrate this,

for the Bourne shell: and for the C shell:

#!/bin/sh #!/bin/csh -f

does=does set does = does

not="" set not = ""

cat << EOF cat << EOF

This here document This here document

$does $not $does $not

do variable substitution do variable substitution

EOF EOF

cat << \EOF cat << \EOF

This here document This here document

$does $not $does $not

do variable substitution do variable substitution

EOF \EOF

Both produce the output:

This here document

does

do variable substitution

This here document

$does $not

do variable substitution

In the top part of the example the shell variables $does and $not are substituted. In the bottom part they are treated as simple text strings without substitution.


Introduction to Unix - 14 AUG 1996
[Next] [Previous] [Up] [Top] [Contents]