HOWTO · Bash

How to Cat EOF in Bash

Learn how to use cat EOF in Bash effectively. This comprehensive guide covers the basics of cat EOF, including file creation, appending text, and writing scripts directly from the command line. Discover practical examples and enhance your Bash scripting skills today.

On this page

A here-document lets a shell pass several lines of text to a command as standard input. In Bash, the shell reads those lines; cat simply copies the input it receives. EOF is a conventional delimiter label, not a command, keyword, or required spelling. You can choose another delimiter, provided the closing line matches it exactly. The shell reads the body before starting the command, so cat does not interpret or remove the delimiter itself.

How a Here-Document Works

The general form is command << DELIMITER, followed by the input and a line containing only the matching delimiter. The shell consumes the here-document before running the command, so the command receives the text through standard input. The closing delimiter must begin at the start of its line unless you use the tab-stripping form described below. Avoid trailing spaces on the closing line because they become part of the delimiter text and stop it matching. The delimiter can be any suitable word that does not occur alone as a body line; EOF and END are conventions that make the closing line easy to recognize.

Run this example:

cat <<'EOF'
A short note
with two lines.
EOF

Output:

A short note
with two lines.

Choose a Delimiter and Control Expansion

With an unquoted delimiter, Bash expands parameters, command substitutions, and arithmetic expressions in the body. For example, cat <<EOF can expand $HOME before cat receives the line. Quote any part of the delimiter, as in <<'EOF', to pass the body literally. This is useful when writing shell code that contains $ characters or when you want the text preserved as typed. The quotes affect how Bash treats the body; they are not part of the delimiter used to close it.

The <<- form removes leading tab characters from the body and closing delimiter. It does not remove spaces, so indentation made with spaces can prevent the closing delimiter from matching. Use tabs intentionally if you choose this form; otherwise, keep the delimiter at the left edge of the line. This tab removal applies to the input sent onward too, which matters if the leading tabs are meaningful data.

Create or Append a File with cat

Redirect standard output to save the text. > creates a file or replaces its existing contents; >> creates the file if needed and adds text at the end if it already exists. Each code example below makes its own temporary directory, so it does not depend on a pre-existing file. Put the redirection on the command line before or after the here-document body as shell syntax allows; the body still ends at its delimiter. Use > only when replacing the old contents is intended.

tmpdir=$(mktemp -d)
trap 'rm -rf "$tmpdir"' EXIT
cat <<'EOF' > "$tmpdir/notes.txt"
First line
Second line
EOF
cat <<'EOF' >> "$tmpdir/notes.txt"
Appended line
EOF
cat "$tmpdir/notes.txt"

Output:

First line
Second line
Appended line

Keep the quoted delimiter when the text should not expand shell variables or substitutions.

Write a Bash Script with a Here-Document

A here-document can write a script as plain text. Creating the file does not run it or make it executable. The example writes a script and then invokes Bash explicitly, so it does not depend on executable permissions. The quoted delimiter is important here: it keeps $ and other shell syntax in the generated script from being expanded by the shell that writes the file.

tmpdir=$(mktemp -d)
trap 'rm -rf "$tmpdir"' EXIT
cat <<'SCRIPT' > "$tmpdir/hello.sh"
#!/usr/bin/env bash
printf '%s\n' 'Hello from the script.'
SCRIPT
bash "$tmpdir/hello.sh"

Output:

Hello from the script.

The quoted SCRIPT keeps the script body literal while the outer shell writes it. The shebang identifies Bash when the file is later run as a program; bash path/to/file selects Bash directly. If you want to run the file by its path instead, set its executable permission first. Creating or appending to a script with cat only writes bytes; it does not validate or execute the script.

Pass Here-Document Input Through a Pipeline

The shell can connect a here-document to a pipeline just like other standard input. Here, grep selects matching lines and tee writes those selected lines to a file while also sending them onward to standard output. This keeps one copy in the terminal and another in the file, while nonmatching input lines are discarded by grep:

tmpdir=$(mktemp -d)
trap 'rm -rf "$tmpdir"' EXIT
cat <<'EOF' | grep 'keep' | tee "$tmpdir/selected.txt"
keep this line
drop this line
keep this one too
EOF

Output:

keep this line
keep this one too

Use a quoted delimiter when the sample text should pass unchanged through the shell before reaching the pipeline.