[BUG] Trailing spaces after backslash break shell line continuation when copying multi-line commands
Description
When copying multi-line shell commands with backslash line continuations from Claude Code output, the pasted commands fail to execute because trailing spaces are inserted between the backslash and the newline character.
In shell, line continuation requires \ to be immediately followed by a newline - any characters in between (including spaces) break the continuation.
Reproduction Steps
- Have Claude Code output a multi-line shell command:
``bash``
echo "line1" && \
echo "line2" && \
echo "line3"
- Copy and paste into terminal
- Expected: All three lines execute as one command
Actual: Command fails with errors:
````
line1
zsh: command not found:
line2
zsh: command not found:
line3
Root Cause Analysis
Inspecting pasted content with xxd:
000000d0: 22e7 acac e4b8 80e8 a18c 2220 2626 205c "........." && \
000000e0: 2020 2020 2020 2020 2020 2020 2020 2020
000000f0: 2020 2020 2020 2020 2020 2020 2020 2020
00000100: 2020 2020 2020 2020 2020 2020 2020 2020
...
00000190: 2020 2020 0a20 2065 6368 6f20 22e7 acac . echo "...
The backslash \ (0x5c) is followed by dozens of space characters (0x20) padding to terminal width, then the newline (0x0a).
This appears to be Claude Code's terminal renderer including display padding characters when text is copied.
Environment
- Claude Code Version: 2.1.11
- Terminal: Termius (SSH client), also reproducible in iTerm2, Terminal.app
- OS: macOS 15.x
- Shell: zsh 5.9
Workaround
Add this to ~/.zshrc to automatically sanitize pasted content:
# Auto-sanitize pasted content (remove \r and trailing spaces)
autoload -Uz bracketed-paste-magic
zle -N bracketed-paste bracketed-paste-magic
zstyle ':bracketed-paste-magic' paste-init paste-sanitize
paste-sanitize() {
PASTED=$(printf '%s' "$PASTED" | sed $'s/\r//g; s/[ \t]*$//')
}
This strips trailing whitespace from each line, restoring proper line continuation behavior.
Impact
- Severity: High - commands fail to execute entirely, not just cosmetic
- Frequency: 100% reproducible with any multi-line command using
\continuation - User impact: Forces manual cleanup or client-side workarounds for basic shell workflows
Related Issues
- #15199 - CLI output formatting artifacts (2-space indent + 80-char wrap)
- #4686 - Copy-paste introduces extra spaces (closed as duplicate)
- #5097 - Copied text contains extra formatting characters (closed as duplicate)
This issue is distinct because it causes functional failure (command execution breaks) rather than just cosmetic problems.
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗