Skip to content

[BUG] Bash Tool Not Respecting Working Directory #11067

Description

@gregdskb

Preflight Checklist

  • I have searched existing issues and this hasn't been reported yet
  • This is a single bug report (please file separate reports for different bugs)
  • 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/username
HOME: /Users/username
OLDPWD: /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 ./config or relative paths

Workarounds Attempted

  1. Using cd command
    Command: cd /Users/username/Projects/myproject && ls project-file.json
    Result: cd command appears to be filtered/stripped out, file not found

  2. Using subshell
    Command: (cd /Users/username/Projects/myproject && pwd)
    Result: Still shows /Users/username

  3. 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:

  1. Each Bash Tool invocation starts a fresh shell in the home directory
  2. The Shell cwd was reset message suggests an attempt to change directory, but this happens AFTER command execution
  3. The Bash Tool guidelines recommend avoiding cd commands, but the tool also doesn't maintain working directory between invocations
  4. This creates a catch-22 where directory-dependent commands cannot be executed

Suggested Fix

Either:

  1. Ensure each bash command starts in the working directory specified in the environment before executing the command
  2. Allow cd commands when necessary for tools that require specific working directories
  3. Provide a parameter to the Bash Tool to specify the working directory for that specific command
  4. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:toolsbugSomething isn't workinghas reproHas detailed reproduction stepsplatform:macosIssue specifically occurs on macOS

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions