Pass Multiline Prompts to Claude Code with Shell HEREDOCs
When you're scripting with claude --print, trying to cram a multi-point prompt into a single quoted string gets ugly fast. Shell HEREDOCs give you clean, readable multi-line prompts with no escaping.
claude --print << 'EOF'
Review the following code for:
1. Security vulnerabilities
2. Performance bottlenecks
3. Missing error handling
Respond with a bullet list for each category.
EOF
Single-quoting the delimiter ('EOF') prevents the shell from expanding variables inside the HEREDOC — useful when your prompt contains dollar signs or backticks. Drop the quotes when you want variable interpolation:
FILE="src/auth/middleware.ts"
CONTEXT="This is a financial application handling PII data."
claude --print << EOF
Context: $CONTEXT
Review $(basename $FILE) for security issues only.
Be brief — one sentence per issue.
$(cat $FILE)
EOF
This pattern works well in Makefiles too:
review:
@claude --print << 'EOF'
Summarise what changed in the last 5 commits and flag anything risky.
$(shell git log --oneline -5)
EOF
HEREDOCs make Claude automation scripts dramatically more readable than escaped single-liners.
Log in to leave a comment.
Before jumping to implementation, describe the problem conversationally and let Claude be your thinking partner. It asks clarifying questions, surfaces tradeoffs you haven't considered, suggests approaches, and pokes holes in your plan — so by the time you say "ok, build it," both of you know exactly what to build and why.
Paste error messages with "why did this fail?" instead of "fix this" to get Claude to diagnose the root cause before applying a fix.
When you need Claude to make changes in one area without affecting another, add negative constraints — "fix the bug but don't change the public API", "refactor the internals but don't create new files", or "update the logic but don't modify any tests." Explicit exclusions prevent Claude from making well-intentioned changes you'll have to undo.