Penguin
Note: You are viewing an old revision of this page. View the current version.
You may have seen scripts containing something like this

cat << EOF What you are about to do is a really really really bad thing. Only do this if you are absolutely sure you want to do it.

Are you absolutely sure that you want to do this? y/N? EOF

(Well, you won't have seen anything like that, because the UnixWay is to shoot first and never ask questions!)

This is what is called a Here Document. The << operator instructs the shell to read input from the current source (most commonly the script file that is being interpreted) and use all those lines as standard input for the command that occured before the <<.

The format (as defined by bash(1)) is

<<-? word

here-document

delimeter

It is possible that word and delimeter may differ (especially if any of the characters in word are quoted), but that's incredibly rare. If you need to know the intracacies, see the bash page. If you put a - after the << then all leading tab characters are stripped from the input lines; this means you can lay out commands as you would normally and they will all be entered without the tabs.

A variant on the here document is the Here String, which uses three <'s to expand a word and supply it to the command on stdin.

foo <<< word


Why would you use such a thing? For documentation, compare:

| cat << EOF
foo bar
foo bar
foo bar foo
EOF | cat foo bar
cat foo bar
cat foo bar foo

Then compare that in a twenty line block. Makes writing the documentation easier and checking it painless (did you mean to mention 'cat' at the beginning of a line and your mind skipped over it because all the lines began with cat?)

Another very important use is automating commands such as FTP that don't otherwise provide a method for scripting;

ftp -n <<EOF open ftp.example.org user anonymous ${USER}@ cd /path/to/file binary passive get file.gz2 EOF

See bash(1).