[BUG] Shell profile echo statements pollute hook stdout, causing "JSON validation failed" errors
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?
When shell profile files (~/.zshrc, ~/.bashrc, etc.) contain unconditional echo statements, these statements execute when Claude Code spawns shell processes to run hooks. The echo output gets prepended to the hook's JSON response, causing "JSON validation failed" errors.
Example error displayed:
⏺ Ran 4 stop hooks
⎿ Stop hook error: JSON validation failed
The root cause is that a line like this in ~/.zshrc:
echo "✅ Shell ready on $(uname -m)"
Causes hook output to become:
✅ Shell ready on arm64
{}
Instead of just:
{}
This is invalid JSON and fails validation.
What Should Happen?
Either:
- Documentation should warn about this common pitfall - that shell profiles should guard echo statements with interactive shell checks
- Or Claude Code could spawn hook shells in a way that skips profile loading (e.g.,
bash --norcorzsh -f), though this may have other implications
Error Messages/Logs
⏺ Ran 4 stop hooks
⎿ Stop hook error: JSON validation failed
Also seen with SessionStart:
⎿ SessionStart:startup hook error
Steps to Reproduce
- Add an unconditional echo to your shell profile:
``bash``
# In ~/.zshrc or ~/.bashrc
echo "Shell ready"
- Configure any command-type hook in
.claude/settings.json:
``json``
{
"hooks": {
"Stop": [{
"matcher": "*",
"hooks": [{
"type": "command",
"command": "echo '{}'"
}]
}]
}
}
- Start a Claude Code session and let it complete (trigger Stop hook)
- Observe "JSON validation failed" error
Workaround
Wrap any echo statements in shell profiles with an interactive shell check:
# Only print for interactive shells (not scripts/hooks)
[[ -o interactive ]] && echo "Shell ready"
Or for bash:
[[ $- == *i* ]] && echo "Shell ready"
Claude Model
Opus
Is this a regression?
I don't know
Last Working Version
_No response_
Claude Code Version
2.1.25 (Claude Code)
Platform
Anthropic API
Operating System
macOS
Terminal/Shell
VS Code integrated terminal (zsh)
Additional Information
This is a subtle issue because:
- The error message doesn't indicate the cause is stdout pollution
- Users don't typically think of their shell profile affecting JSON hook output
- The shell profile may have worked fine for years until hooks are introduced
Related issues:
- #18215 - Hook documentation contradicts stdout handling
- #19057 - Presence of ~/.bashrc triggers shell state capture issues
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗