← Back to Bash Course | Chapter 3: Control Flow | Lesson 13 of 14

Here Documents (<<EOF)

A here document is like reading a whole paragraph out loud to someone until you say a secret stop word. Everything between the start and the stop word is handed to the command as one message.

In this page:

  1. Here Documents (<<EOF)
Syntax
bash
command <<DELIMITER
text line 1
text line $variable
DELIMITER

Here Documents (<<EOF)

A here document lets you feed multiple lines of literal text to a command's standard input, starting with <<EOF and ending at a line containing just the matching delimiter (EOF by convention, but any word works).

Variables inside are expanded unless the delimiter is quoted, like <<EOF. Here documents are commonly used to write multi-line text into a file without many separate echo calls.

Note: Quote the delimiter, <<EOF, to prevent variable expansion inside the here document.

Example: Here Documents (<<EOF)

bash
#!/bin/bash
name="World"

cat <<EOF
Hello, $name!
This is a multi-line
here document.
EOF
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Indenting the closing delimiter with spaces, so EOF is not recognised and the here document never ends.
  2. Using <<EOF and being surprised that variables are expanded; quote the delimiter, <<EOF, to prevent it.
  3. Writing extra text on the closing delimiter line.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.