[BUG] Bash tool duplicates stdout in model context when command exits non-zero

Resolved 💬 2 comments Opened Mar 6, 2026 by alex-raw Closed Apr 3, 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?

When a Bash tool command exits with a non-zero exit code, its stdout appears twice in the model's context. Exit code 0 produces a single copy as expected.

This is not specific to any script, language, or shell — it reproduces with minimal commands:

| Command | Exit code | Copies in context |
|---|---|---|
| echo "hello" | 0 | 1 (correct) |
| echo "hello" && exit 1 | 1 | 2 (bug) |
| python3 -c "print('hello'); exit(1)" | 1 | 2 (bug) |
| bash -c 'echo "hello" && exit 1' | 1 | 2 (bug) |
| echo "hello" && exit 2 | 2 | 2 (bug) |
| grep "no_match" /etc/hostname | 1 (no output) | 0 (correct, nothing to double) |

Verified the duplication is in the tool result, not in the command itself: redirecting 1>/tmp/out.txt 2>/tmp/err.txt shows a single copy on stdout, empty stderr. The Bash tool result presented to the model contains two copies.

What Should Happen?

Bash tool output should appear exactly once in the model context regardless of exit code.

Error Messages/Logs

# Running: echo "hello" && exit 1
# Model receives in tool result:
hello

hello

Steps to Reproduce

  1. Start a Claude Code session
  2. Have the model run: echo "test output" && exit 1
  3. Observe the tool result contains "test output" twice
  4. Compare with: echo "test output" (exit 0) — single copy

Claude Model

Opus

Is this a regression?

Yes, this worked in a previous version

Last Working Version

Unknown

Claude Code Version

2.1.63

Platform

Anthropic API

Operating System

Other Linux

Terminal/Shell

Other

Additional Information

  • OS: Arch Linux, kernel 6.18.13-arch1-1
  • Shell: zsh
  • Terminal: Alacritty

View original on GitHub ↗

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