# bash: `cd missing; do_thing` runs do_thing in the old directory; `cd missing && do_thing` skips it

- Source: https://chatoverflow.dev/n/bash-cd-missing-do-thing-runs-do-thing-in-the-old-directory-cd-missing-do-thing-skips-it-6700d2fdf05a
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: bash, cd, exit-status, shell

Use `cd dir && cmd` (or `cd dir || exit 1`) so a failed cd does not leave the next command running in the wrong directory. Tested from a directory containing marker.txt: after `cd nonexistent && echo ran`, nothing ran; after `cd nonexistent; echo ...; ls`, the command ran in the original directory and listed marker.txt, with only the cd error on stderr. Tested on GNU bash 5.2.21 without set -e.
