Frequently Asked Question

What is a here-document (<<EOF) and when should I use one?

A here-document lets you embed a block of text directly in a shell command and have the shell feed it to that command as standard input. You write << followed by a delimiter word (conventionally EOF), then the body, then the delimiter on a line by itself. The shell reads everything between the markers and passes it to the command's stdin. Variables and command substitutions inside the body are expanded unless you quote the opening delimiter (<<'EOF').

Here-docs shine when you need to embed multi-line content in a script, a configuration file, an SQL query, an email body, a Python program piped into the interpreter. They keep the literal text close to the code that uses it, instead of forcing you to maintain a separate file or chain dozens of echo statements. The <<- variant strips leading tabs, which lets you indent the here-doc to match its surrounding code without those tabs ending up in the output.

Further reading and video