Bash Heredoc Examples

Basic heredoc (write to file)

cat << EOF > output.txt
Line 1
Line 2 with a $VARIABLE expanded
EOF

Suppress variable expansion (single-quote the delimiter)

cat << 'EOF' > output.txt
This $VARIABLE won't be expanded
Neither will $(commands)
EOF

Indented heredoc (<<- strips leading tabs)

if true; then
	cat <<- EOF
	This line's leading tab is stripped
	EOF
fi

Only works with literal tab characters, not spaces.

Pipe a heredoc into a command

grep "search" << EOF
line one
line two with search in it
EOF

Heredoc into a variable

read -r -d '' MY_VAR << 'EOF'
Multi-line
string content
EOF

Quick reference

SyntaxBehavior
<< EOFExpand $vars and $(cmds)
<< 'EOF'No expansion (literal)
<<- EOFStrip leading tabs

The delimiter (EOF) can be any word — END, HEREDOC, SQL, etc. — just match it exactly on the closing line (no leading/trailing spaces unless using <<- with tabs).