December 5, 2026

Bash Heredoc Examples: Quoted vs Unquoted, Tabs vs Spaces, and the EOF That Never Ends

A laptop terminal showing a block of indented code at night

Two heredocs in a test script held the same line: Hello, $name. Today is $(date ...). Cost: \$5. The first printed Hello, world. Today is Thursday. Cost: $5. The second printed the line exactly as typed, dollar signs, parentheses, backslash and all. The only difference was a pair of single quotes around the word EOF. Heredocs, the <<EOF blocks that feed multi-line text into a command, are everywhere in shell scripts, from writing config files to sending commands over SSH, and most of the bugs in them come from a handful of rules. Every example below ran in Bash 5.2.

Video: You Suck at Programming on YouTube

What a heredoc is

Multi-line input from inside the script

A heredoc, short for “here document,” supplies a command’s standard input from lines written directly in the script, up to a closing delimiter:

cat <<EOF
first line
second line
EOF

The word after <<, here EOF, can be anything; EOF and END are conventions. Everything until that word appears again is sent to cat.

The closing line has to be exact

The POSIX shell specification, section 2.7.4, says the heredoc continues “until there is a line containing only the delimiter and a , with no characters in between.” No leading spaces, no trailing spaces, nothing else on the line.

An open cream-colored envelope on a white background

Unquoted: the shell expands everything

Variables, commands and arithmetic

name=world
cat <<EOF
Hello, $name. Today is $(date +%A -d 2026-01-01). Cost: \$5
EOF

Output:

Hello, world. Today is Thursday. Cost: $5

$name became world, the $(date ...) command ran and printed the weekday for January 1, 2026, and \$5 became a literal $5. With an unquoted delimiter, the heredoc body behaves much like a double-quoted string.

When that’s useful

Writing a config file with values filled in from variables is the classic case:

cat > out.conf <<EOF
port=8080
user=$name
EOF

The file came out with user=world.

Mixing expanded and literal text

An unquoted heredoc can still keep individual characters literal with a backslash:

user=peter
cat <<EOF
User: $user, literal: \$HOME, backtick literal: \`date\`
EOF

printed User: peter, literal: $HOME, backtick literal: `date`. That’s handy when most of a block needs variables filled in but a few dollar signs must survive, such as a config file that refers to its own environment variables.

Quoted: nothing is expanded

Quoting the delimiter turns expansion off

cat <<'EOF'
Hello, $name. Today is $(date +%A). Cost: \$5
EOF

Output, character for character:

Hello, $name. Today is $(date +%A). Cost: \$5

The POSIX specification states the rule: if any part of the delimiter word is quoted, “the here-document lines shall not be expanded.” 'EOF', "EOF" and even \EOF all count.

When that’s essential

Writing a script, a template or anything containing $ that should stay as written needs the quoted form. A heredoc that generates another shell script with an unquoted EOF will substitute the generating script’s variables into it, almost never what was intended, and usually empty strings since those variables aren’t set yet.

Brown wooden triangle ruler

Indenting with <<-

Tabs are stripped

Heredocs inside if blocks and functions look awkward because the body and delimiter must start at the left margin. The <<- form fixes that, but only for tabs. POSIX says that with <<-, “all leading characters shall be stripped from input lines,” including the delimiter line. A block indented with tabs:

if true; then
    cat <<-EOF
    indented with tabs
        two tabs
    EOF
fi

printed:

indented with tabs
two tabs

Every leading tab was removed, even the second one, so relative indentation inside the text is lost.

Spaces are not stripped

The same block indented with four spaces instead of a tab broke completely:

bad2.sh: line 5: warning: here-document at line 2 delimited by end-of-file (wanted `EOF')
bad2.sh: line 6: syntax error: unexpected end of file

The indented EOF didn’t count as the delimiter, so Bash kept reading to the end of the file, swallowing the closing fi. Many editors convert tabs to spaces automatically, which is why a <<- heredoc that worked yesterday can fail after a reformat.

The EOF that never ends

A warning, not an error

A plain heredoc whose closing EOF had two spaces in front of it:

cat <<EOF
hello
  EOF

produced:

bad.sh: line 3: warning: here-document at line 1 delimited by end-of-file (wanted `EOF')
hello
  EOF

and an exit status of 0. Bash used everything to the end of the file as the heredoc, including the line meant to close it, printed a warning and carried on. In a longer script, every command after that point would silently become text fed to cat instead of running.

Spotting it

That warning is the signal: delimited by end-of-file (wanted 'EOF') means the closing line never matched. Checking it for stray spaces, a trailing space or a Windows line ending fixes nearly every case.

When the text itself contains EOF

The opposite failure happens when the body contains a line that matches the delimiter. A help message that mentioned EOF on a line of its own:

cat <<EOF
usage notes
EOF
this line was meant to be inside
EOF

stopped at the first EOF. Bash printed usage notes, then tried to run the next line as a command: this: command not found, followed by EOF: command not found, and the script exited with status 127. Changing the delimiter to something that can’t appear in the text, such as END_OF_HELP, printed all three lines correctly. Long or generated heredocs are safer with a distinctive delimiter than with the habitual EOF.

A close-up of some rolls of toilet paper

Here-strings and loops

One line with <<<

Bash also has here-strings, a one-line variant. The bash manual says the word “undergoes tilde expansion, parameter and variable expansion, command substitution,” arithmetic expansion and quote removal, and that the result “is supplied as a single string, with a newline appended.”

wc -w <<< "one two three"
read -r a b <<< "first second"
echo "a=$a b=$b"

printed 3, then a=first b=second. Here-strings are a tidy way to feed a variable into read or a command without echo and a pipe.

Why heredocs beat pipes for loops

A while read loop fed by a heredoc counted three lines correctly: count=3. The same loop fed by a pipe, printf 'a\nb\nc\n' | while read -r l; do c2=$((c2+1)); done, left c2 at 0 afterward. In Bash, each part of a pipeline runs in a subshell by default, so the loop’s variable changes vanish when it ends. A heredoc or here-string runs the loop in the current shell, and the counter survives.

Common uses

Writing files that need root

sudo cat > /etc/app.conf <<EOF fails for a normal user, because the > redirection is performed by the user’s own shell before sudo ever runs. Sending the heredoc to sudo tee puts the writing inside the elevated command:

sudo tee /etc/app.conf > /dev/null <<'EOF'
port=8080
EOF

Running commands on another machine

ssh host 'bash -s' <<'EOF' sends a block of commands to a remote shell. Quoting the delimiter matters doubly here: with <<EOF, variables expand on the local machine before anything is sent; with <<'EOF', they expand on the remote one.

Feeding another language

Heredocs are the simplest way to embed a short program in a shell script. python3 - <<'EOF' runs the block as a Python script; the test printed python got (3, 13) args: ['-'], with the - telling Python to read its program from standard input. The same pattern runs SQL through a command-line client:

sqlite3 :memory: <<'EOF'
create table t(x);
insert into t values (1),(2),(3);
select count(*), sum(x) from t;
EOF

printed 3|6. The quoted delimiter matters again: SQL and Python both use characters, such as $ and backticks, that the shell would otherwise try to interpret.

Capturing a heredoc in a variable

Wrapping a heredoc in command substitution stores its text:

msg=$(cat <<'EOF'
Line one keeps $HOME literal
Line two
EOF
)

$msg held both lines, with $HOME untouched. Command substitution strips the final newline, so the variable ended at Line two, a detail that matters when the text is later written to a file.

Functions read heredocs too

A shell function’s standard input can come from a heredoc just like any command’s. A small function that read lines and echoed them back, called as f <<EOF with two lines, printed got: alpha and got: beta. That makes it easy to write reusable functions that take a block of text, such as a list of packages or hostnames, without temporary files.

Choosing the form

Need Form
Fill in variables <<EOF
Keep text exactly as written <<'EOF'
Indent inside a block <<-EOF with tabs only
One line of input <<<

Sources

  • Shell Command Language, section 2.7.4 Here-Document, The Open Group Base Specifications (POSIX): https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html
  • bash(1), Linux manual page: https://man7.org/linux/man-pages/man1/bash.1.html

920 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *