[BUG] Shell initialization (cd ~ in .bashrc) breaks workspace boundary
Description
Claude Code's Bash tool executes commands outside the intended workspace when user's .bashrc or .zshrc contains directory change commands (e.g., cd ~).
Root Cause
When the Bash tool starts its persistent shell session, it sources the user's shell configuration file (.bashrc, .zshrc, etc.). If this configuration contains commands that change the working directory (like cd ~), the shell starts in the wrong directory despite Claude Code's working directory being set correctly.
Reproduction Steps
- Add
cd ~to the top of your~/.bashrcfile - Start Claude Code in a project directory:
/home/user/my-project/ - Run any Bash command via CC that uses relative paths (e.g.,
find . -name "*.md") - Observe that the command executes from
/home/user/instead of/home/user/my-project/
Expected Behavior
Bash commands should execute within the workspace directory regardless of shell initialization scripts.
Actual Behavior
- First Bash command executes from wrong directory (e.g., home directory)
- System then resets cwd with message: "Shell cwd was reset to /home/user/my-project/"
- This causes CC to search/modify files outside the intended workspace
- Violates the fundamental workspace boundary principle
Impact
- Security concern: CC can inadvertently access/modify files outside workspace
- Data corruption risk: Commands like
findor file operations may affect wrong directories - Confusing behavior: Users expect CC to operate only within workspace
- Related to existing issues: #5038, #11067, #7442, #3275
Current Workaround
Remove directory-changing commands from shell initialization files.
Suggested Solutions
- Start Bash in workspace directory explicitly: After sourcing shell config, force
cdto workspace directory - Sandbox shell initialization: Source a minimal environment instead of full user config
- Detect and warn: Check if cwd changed after sourcing config and warn user
- Document best practices: Add warning in docs about shell config affecting workspace boundaries
Environment
- OS: Ubuntu Linux (WSL2)
- Shell: bash
- Claude Code version: Latest (as of December 2024)
- Configuration:
cd ~at line 1 of~/.bashrc
Additional Context
This is a subtle issue that many users might not notice until CC starts operating outside their workspace. The "Shell cwd was reset" message indicates the system detects this, but it happens after the command executes in the wrong directory.
Users coming from various shell configurations (especially those with custom workspace management) may unknowingly have this problem without understanding why CC behaves unexpectedly.
This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗