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:
Syntax
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)
#!/bin/bash
name="World"
cat <<EOF
Hello, $name!
This is a multi-line
here document.
EOF
Login to try C/C++/Java/PHP code in the editor
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
- Indenting the closing delimiter with spaces, so
EOFis not recognised and the here document never ends. - Using
<<EOFand being surprised that variables are expanded; quote the delimiter,<<EOF, to prevent it. - Writing extra text on the closing delimiter line.
🔒
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: