Shell Scripts and Here Documents

Tip of the Trade: A here document, or heredoc, is one way to get text input into a script without feeding it from a separate file. If the data or script is complicated, however, keeping the two separate is generally a better idea.

A here document (or heredoc) is a way of getting text input into a script without
having to feed it in from a separate file. If you've got a small quantity of
data you don't expect to change often, it's a quick and tidy way of
keeping script and data together. For more complicated data or scripts,
separating the two is still usually a better idea.

The basic version of this in bash looks like this:

#!/bin/bash
cat << EOF
several lines of
my data
listed here
EOF

which will output

several lines of
my data
listed here

You can use any label (although EOF is standard)  just make sure it
matches at both start and end.

There are a couple of useful variations on this format. Just using a plain
label, as above, means the shell will interpret any special characters.
This would substitute your current directory for $PWD:

#!/bin/bash
cat << EOF
current directory is:
$PWD
EOF

However, with some data sets this can cause problems, especially if you don't
remember that it happens! To disable interpretation, use single or double quotes:

#!/bin/bash
cat << 'EOF'
current directory variable is:
$PWD
EOF

This will output $PWD verbatim:

current directory variable is:
$PWD

By and large, it's best to disable shell interpretation by default and to take the quotes out if your data definitely requires it.

Another useful trick is to use a minus
sign after the <<. This will ignore tab characters (Note: not
all whitespace!) at the start of a line, so you can indent your data to
increase the readability of your script:

#!/bin/bash
cat <<- 'EOF'
lots of my
data will be printed
out without tabs!
EOF

You can also use heredocs in other programming languages, including Perl,
PHP and Python. Be aware, however, that quotes may affect escaping of special characters
in different ways in different languages.

Juliet Kemp has been messing around with Linux systems, for financial reward and otherwise, for about a decade. She is also the author of "Linux System Administration Recipes: A Problem-Solution Approach" (Apress, 2009).

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.