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.
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.
The basic version of this in bashlooks like this:
#!/bin/bash cat |
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 |
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 |
This will output $PWDverbatim:
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 |
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).