Shell Scripting / Bash Tricks
Stop variables from expanding when you only want the literal string
🧩 The Challenge
You are writing a script to generate a config file and realize your shell is greedily trying to evaluate variables that don’t belong to the script yet. It is infuriating when your heredoc turns into a mess of empty strings because it tried to find a local variable that wasn’t there.
💡 The Fix
Just quote the heredoc delimiter to turn off variable expansion entirely within that block. It keeps the text literal so the remote system can handle the variables instead.
cat <<'EOF' > /etc/remote-config.conf
export API_KEY=${SECRET_TOKEN}
EOF
⚙️ Why It Works
Adding those single quotes around the EOF tag tells the shell to ignore everything until it hits the delimiter without running it through the expander. This saves you from having to escape every single dollar sign with a backslash.
🚀 Pro-Tip: Use this for generating cron jobs or service files where you need literal placeholders like $HOME or $1 to stay exactly as written.
Linux Tips & Tricks | © ngelinux.com | 8/11/2026
