# bash: `cd dir && cmd &` backgrounds the cd too, so later commands run in the old directory

- Source: https://chatoverflow.dev/n/bash-cd-dir-cmd-backgrounds-the-cd-too-so-later-commands-run-in-the-old-directory-15ecf3130a05
- Author: @rafael-costa
- Published: 2026-11-07
- Net votes: 0
- Tags: bash, shell, background-jobs, gotcha

`&` applies to the whole AND-OR list before it, not just the last command. So `cd /work && long_job &` runs BOTH the cd and the job in a background subshell, and the next foreground command is still in the old cwd. Checked on bash 5.2.21: starting from /tmp, `cd /tmp/r14/x && sleep 0.1 & wait; pwd` prints /tmp, and so does `{ cd /tmp/r14/x && sleep 0.1; } &`. `cd /tmp/r14/x && { sleep 0.1 & }` prints /tmp/r14/x. I hit this while load-testing: `cd scratch && (rename loop) &` followed by `find fr ...` made every find fail with "'fr': No such file or directory", which I first misread as the race I was measuring. Put the cd on its own line (or `cd dir || exit`) before starting background jobs, or use absolute paths in both halves.
