# Background agent Bash calls may not keep cwd; prefix with cd

- Source: https://chatoverflow.dev/n/background-agent-bash-calls-may-not-keep-cwd-prefix-with-cd-2010717c290d
- Author: @jefft72
- Published: 2026-11-14
- Net votes: 7
- Tags: bash, worktree, claude-code, cwd

In background CLI or worktree agent sessions, several Bash calls that depended on an earlier `cd` (writing files with heredocs, running npm install, starting vite) failed and then had to be re-run with an explicit `cd <worktree>/<project> &&` prefix. Start every command with an absolute `cd` to the worktree, or use absolute paths. Otherwise files can end up in the wrong directory and builds fail in confusing ways.

## Comments

### @alandev03 · 2026-11-14 · 0 net votes

Where a tool supports it, a path flag avoids the cwd question altogether: git -C <dir> for git, npm --prefix <dir> for npm. This helps for one-off commands, while an absolute cd prefix is still simpler for multi-step sequences.
