Heredoc

2 snippets across 2 stacks - Bash & Linux, Python

SHBash & Linux

Here Document

SH · Redirects & Pipes
Syntax
command <<DELIMITER
text
DELIMITER
Example
cat <<EOF > /etc/nginx/conf.d/app.conf
server {
    listen 80;
    server_name app.example.com;
    location / {
        proxy_pass http://localhost:3000;
    }
}
EOF

# Suppress variable expansion:
cat <<'EOF'
Use $HOME to reference your home directory
EOF

Note Variables and commands are expanded inside a here document by default. Quoting the delimiter ('EOF') disables expansion, which is useful when writing scripts or config files that contain $ characters. <<- strips leading tabs (not spaces) for cleaner indentation.

PYPython

Multiline Strings

PY · Strings
Syntax
"""text
spanning lines"""
Example
query = """
    SELECT name, email
    FROM users
    WHERE active = true
""".strip()
print(query)
Output
SELECT name, email
    FROM users
    WHERE active = true

Note Triple-quoted strings preserve all whitespace and newlines. Use textwrap.dedent() or .strip() to control leading/trailing space.

Frequently asked questions

How does Bash & Linux handle heredoc?
This task is covered in 2 stacks on this page: Bash & Linux, Python. The "Here Document" snippet in Bash & Linux uses `command <<DELIMITER`.
Which command does the Bash & Linux example use?
The "Here Document" snippet uses `command <<DELIMITER`, from the Redirects & Pipes section of the Bash & Linux cheat sheet.
Which stacks cover "heredoc" on this page?
Bash & Linux, Python. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Here Document": Variables and commands are expanded inside a here document by default. Quoting the delimiter ('EOF') disables expansion, which is useful when writing scripts or config files that contain $ characters. <<- strips leading tabs (not spaces) for cleaner indentation.