Heredoc

A here document or heredoc is a section of source code file treated as a separated file. It also refers to a way to form a multiline string literals, preserving line breaks and other whitespace (including indentation) in a text.

In Unix shells, heredoc are used to to output commands or text preserving whitespaces:

$ cat << EOF > ./my_file.txt
heredoc> This file has multiple lines.
heredoc> It is in directory "$PWD".
heredoc> EOF

The command above uses heredoc to output two lines to a file called my_file.txt. The command "$PWD" is expanded at execution.

EOF is used as the delimiting identifier. By convention it is often EOF or ENDin all-uppercase but it can be anything, as long as the first and last line use the same identifier.

$ cat my_file.txt
This file has multiple lines.
It is in directory "/home/user".

It is possible to not expand commands by surrounding EOL with single quotes: << 'EOF'.

More informations

See also Heredoc with Ruby.

Links to this page