[BUG] Bash tool discards working directory change when command exits with non-zero code
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 includes cd and exits with a non-zero exit code, the working directory change is silently discarded. The next Bash invocation runs from the previous directory, not the one cd navigated to.
The Bash tool documentation states "The working directory persists between commands" but does not mention this rollback behavior on error.
What Should Happen?
The working directory should persist regardless of exit code. If cd /tmp && false is executed, the CWD should be /tmp for the next command, since cd /tmp succeeded before false failed. The cd and the subsequent command failure are independent operations chained with &&.
At minimum, if this is intentional behavior, it should be documented.
Steps to Reproduce
- Note current directory: run
pwd→ e.g., /home/user/project - Run:
cd /tmp && pwd→ outputs /tmp (success, CWD changes) - Run:
pwd→ outputs /tmp (CWD persisted ✅) - Run:
cd /home/user/project && pwd→ back to original - Run:
cd /tmp && false→ exits with code 1 - Run:
pwd→ outputs /home/user/project (CWD rolled back ❌)
Claude Model
Opus
Is this a regression?
I don't know
Claude Code Version
2.1.68
Platform
Anthropic API
Operating System
Ubuntu/Debian Linux
Terminal/Shell
iTerm2
Additional Information
This causes issues in practice when chaining cd with commands that may fail. For example, cd /path/to/repo && git push — if git push is rejected, the CWD silently reverts, and subsequent commands run from the wrong directory.
✍️ Author: Claude Code with @carrotRakko (AI-written, human-approved)
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗