Introduction to Unix |
|
![]() |
![]() |
4.4. Here Documents¶
Linux for Programmers and Users, Section 5.6.2 and 5.17
This rather peculiar metacharacter (<<) allows us to provide complex, multi-line input to a command. Perhaps its most common usage is in a shell script used with the cat command to display a lengthy description to the user.
An example might illustrate its usage best:
$ cat << STOP
> Today, we hope that you learn a great deal about Unix.
> We are using Linux as our Unix system.
> There are many versions of Unix systems, including:
> UNIX, Solaris, BSD, AIX, HP-UX and Linux.
> References to UNIX, as it used to be spelled, are considered
> references to the UNIX from AT&T Bell Labs. Unix is a
> generic term refering to all Unix like systems.
> STOP
Today, we hope that you learn a great deal about Unix.
We are using Linux as our Unix system.
There are many versions of Unix systems, including:
UNIX, Solaris, BSD, AIX, HP-UX and Linux.
References to UNIX, as it used to be spelled, are considered
references to the UNIX from AT&T Bell Labs. Unix is a
generic term refering to all Unix like systems.
In the above example, the > characters at the beginning of each line are produced by the cat command, which is reading from standard input. Those characters will not be present in shell script.
Note
- The Here Documents metacharacter produced the end-of-file character. It was not necessary to type Cntrl-d above.
- In the above example, STOP is an identifier. Any string may be used as the terminal string.
4.4.1. Strip Leading Tabs¶
The BASH shell provides an extra feature to Here Documents. When a dash is
added to the metacharacters, <<-
, then leading tab characters are
removed in the displayed information. This might might make formating in
shell script look neater because the text can be indented.
$ cat <<- STOP
> An indented line ...
> or two ...
> STOP
An indented line ...
or two ...