[BUG] Bash Tool Not Respecting Working Directory
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?
The Bash Tool consistently executes commands from the user's home directory instead of the working directory specified in the environment, despite the environment indicating a different working directory and Shell cwd was reset messages appearing after command execution
What Should Happen?
When the environment specifies a working directory of /Users/username/Projects/myproject, bash commands should execute from that directory
Error Messages/Logs
All bash commands execute from `/Users/username` (home directory), regardless of the working directory specified in the environment. After each command completes, a message appears: `Shell cwd was reset to /Users/username/Projects/myproject`, but subsequent commands still execute from the home directory
Steps to Reproduce
Test Cases to Reproduce
Test 1: Basic pwd check
Command: pwd
Expected output: /Users/username/Projects/myproject
Actual output: /Users/username
Post-execution message: Shell cwd was reset to /Users/username/Projects/myproject
Test 2: File existence check
Command: ls project-file.json
Expected: File listed (exists in /Users/username/Projects/myproject/)
Actual: ls: project-file.json : No such file or directory
Reason: Command runs from /Users/username instead of project directory
Test 3: Environment variable check
Command: echo "PWD: $PWD" && echo "HOME: $HOME" && echo "OLDPWD: $OLDPWD"
Output:PWD: /Users/usernameHOME: /Users/usernameOLDPWD: /Users/username/Projects/myproject
Post-execution message: Shell cwd was reset to /Users/username/Projects/myproject
Analysis: PWD shows home directory, OLDPWD shows the intended working directory
Test 4: Sequential commands
Command 1: pwd
Result: /Users/username
Message: Shell cwd was reset to /Users/username/Projects/myproject
Command 2: pwd (immediately after)
Result: /Users/username (still wrong)
Message: Shell cwd was reset to /Users/username/Projects/myproject
Test 5: Absolute path verification
Command: ls /Users/username/Projects/myproject/project-file.json
Result: SUCCESS - file exists at absolute path
Command: ls project-file.json
Result: FAIL - No such file or directory
Conclusion: Files exist in the project directory, but commands don't execute from there
Claude Model
Sonnet (default)
Is this a regression?
I don't know
Last Working Version
_No response_
Claude Code Version
Claude Code v2.0.33
Platform
Anthropic API
Operating System
macOS
Terminal/Shell
VS Code integrated terminal
Additional Information
This issue prevents commands that require execution from a specific directory from working
Examples:
- Build tools that look for configuration files in the current directory
- Package managers (
npm,pip, etc.) that need to run from project root - Git commands when repository isn't at home directory
- CLI tools that use
./configor relative paths
Workarounds Attempted
- Using
cdcommand
Command: cd /Users/username/Projects/myproject && ls project-file.json
Result: cd command appears to be filtered/stripped out, file not found
- Using subshell
Command: (cd /Users/username/Projects/myproject && pwd)
Result: Still shows /Users/username
- Using absolute paths with
git -C
Command: git -C /Users/username/Projects/myproject status
Result: SUCCESS - This workaround works for git commands
Analysis
The issue appears to be that:
- Each Bash Tool invocation starts a fresh shell in the home directory
- The
Shell cwd was resetmessage suggests an attempt to change directory, but this happens AFTER command execution - The Bash Tool guidelines recommend avoiding
cdcommands, but the tool also doesn't maintain working directory between invocations - This creates a _catch-22_ where directory-dependent commands cannot be executed
Suggested Fix
Either:
- Ensure each
bashcommand starts in the working directory specified in the environment before executing the command - Allow
cdcommands when necessary for tools that require specific working directories - Provide a parameter to the Bash Tool to specify the working directory for that specific command
- Maintain a persistent shell session that respects directory changes
Additional Context
The Bash Tool documentation states: "Try to maintain your current working directory throughout the session by using absolute paths and avoiding usage of cd." However, when tools require execution from a specific directory and don't support absolute path parameters, this guidance cannot be followed
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗