Sunday, January 31, 2010

Shell Scripts and Here Documents

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:

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.

No comments:

Post a Comment