Bad substitution error when using heredoc/EOF syntax in bash commands

Resolved 💬 3 comments Opened Jan 16, 2026 by holdforever2021-bot Closed Jan 19, 2026

Preflight Checklist

  • [x] I have searched existing issues and this hasn't been reported yet
  • [x] This is a single bug report (please file separate reports for different bugs)
  • [x] I am using the latest version of Claude Code

What's Wrong?

Claude Code crashes with "Bad substitution" error when attempting to write multi-line files using heredoc syntax.

Error message:
"Error: Failed to parse command: Bad substitution: strategy"
and
"Error: Failed to parse command: Bad substitution: orderId"

This happens when Claude Code generates bash commands like:
cat > file.js << 'EOF'
[multi-line content with variables like ${strategy} or ${orderId}]
EOF

The command fails before execution. Rewind doesn't help - the same error repeats.

What Should Happen?

Claude Code should successfully write multi-line files to the filesystem, either by:

  1. Properly escaping variables in heredoc syntax
  2. Using alternative methods (echo, printf, or file editing tools)
  3. Detecting the substitution issue and auto-correcting the approach

Error Messages/Logs

Steps to Reproduce

  1. Start Claude Code: npx @anthropic-ai/claude-code
  2. SSH to a remote server
  3. Ask Claude Code to create or modify a JavaScript file with template literals or variables (e.g., "Add email notifications to my Node.js webhook server")
  4. Claude Code attempts heredoc syntax with ${variable} patterns
  5. Error: "Bad substitution" - command fails
  6. /rewind doesn't fix - same error on retry

Claude Model

Not sure / Multiple models

Is this a regression?

I don't know

Last Working Version

_No response_

Claude Code Version

v2.1.9

Platform

Anthropic API

Operating System

macOS

Terminal/Shell

Terminal.app (macOS)

Additional Information

_No response_

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗