Bash

Heredoc with Variable Interpolation Control

admin by @admin ADMIN
1d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Heredocs are the cleanest way to embed multi-line text. Unquote the delimiter to allow variable expansion; QUOTE it to keep the body literal (no $foo expansion).
Bash
Raw
name="Alice"

# Quoted delimiter: NO expansion (good for embedding scripts or shell snippets)
cat <<'EOF'
Hello $name — this $variable stays literal.
EOF

# Unquoted: full expansion
cat <<EOF
Hello $name — interpolated.
Current dir: $(pwd)
Date: $(date)
EOF

# <<-EOF strips leading tabs (handy when indenting inside if/for)
if true; then
    cat <<-'EOF'
	Indented heredoc — leading tabs stripped, single quotes prevent expansion.
	Useful inside functions / conditional blocks.
	EOF
fi
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.