SSH commands omit cd when local working directory matches remote target path
Bug Summary
Claude Code omits cd from SSH commands when the local working directory happens to match the target remote directory. The system prompt instruction to "avoid usage of cd" is incorrectly applied to remote SSH shells, where the local working directory is irrelevant — the remote shell always starts in ~.
Environment
- Claude Code version: 2.1.27
- Model: claude-opus-4-5-20251101 (also reproduced on Sonnet)
- Platform: Linux (Proxmox 6.8.12-15-pve)
- Date: 2026-01-31
Reproduction
Prerequisites
- Passwordless SSH to localhost (
ssh-copy-id localhostif needed)
Setup
# Create test directories with marker files
mkdir -p /tmp/ssh-cd-bug-test/{target,other}
echo "MARKER-TARGET" > /tmp/ssh-cd-bug-test/target/proof.txt
echo "MARKER-OTHER" > /tmp/ssh-cd-bug-test/other/proof.txt
Test 1 — Bug case (local cwd matches remote target)
cd /tmp/ssh-cd-bug-test/target
claude # start a fresh session
Ask Claude: "ssh to localhost, cd to /tmp/ssh-cd-bug-test/target, and cat proof.txt"
Expected command:
ssh $USER@localhost "cd /tmp/ssh-cd-bug-test/target && cat proof.txt"
Actual command (bug):
ssh $USER@localhost "cat proof.txt"
Result: cat: proof.txt: No such file or directory — because the remote shell starts in ~, not in the target directory.
Test 2 — Control (local cwd does NOT match remote target)
Still in /tmp/ssh-cd-bug-test/target, ask Claude: "ssh to localhost, cd to /tmp/ssh-cd-bug-test/other, and cat proof.txt"
Result: Claude correctly generates:
ssh $USER@localhost "cd /tmp/ssh-cd-bug-test/other && cat proof.txt"
This works because the paths don't match, so Claude doesn't "optimize away" the cd.
Cleanup
rm -rf /tmp/ssh-cd-bug-test
Root Cause Analysis
The system prompt for Bash tool usage contains:
"Try to maintain your current working directory throughout the session by using absolute paths and avoiding usage of cd."
When the local cwd (/tmp/ssh-cd-bug-test/target) matches the remote target path, Claude treats the remote SSH shell as if it shares the local working directory and omits the cd. This is incorrect — an SSH session always starts in the remote user's home directory regardless of the local cwd.
Evidence of thinking/output disconnect
During debugging, Claude's thinking block contained the correct reasoning:
"I need to make sure I include thecdcommand this time. The correct pattern is:ssh user@host "cd /path && git pull""
But the actual tool call still omitted the cd:
{"command": "ssh user@host \"git pull\""}
This happened repeatedly (4+ times in a single session), even after explicit correction. The system prompt instruction appears to override Claude's own reasoning at the tool-call generation stage.
Impact
- Commands executed on the wrong remote directory (home dir instead of target)
git pullin wrong directory →fatal: not a git repository- File operations on wrong directory could be destructive
- Bug is silent — Claude reports it ran the command "in" the target directory
- Persists across sessions, models, and even after clearing all caches/history
Suggested Fix
The "avoid cd" instruction should be scoped to local Bash commands only. When generating commands inside an ssh invocation, cd to the target directory is required and should not be suppressed. Possible approaches:
- Add an exception to the system prompt: "When constructing SSH commands, always include
cdto the target directory since the remote shell starts in~." - Detect that the command is wrapped in
sshand exempt it from thecdavoidance rule. - Reframe the instruction as "use absolute paths" rather than "avoid
cd" — absolute paths naturally work for local commands, whilecdremains necessary for SSH.
This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗