Skip to content

cd-without-exit

Shellsuspiciousno fixdefault: warnshell/shellcheck

Reports cd, pushd or popd whose failure is not handled, as in cd dir || exit. It is not reported under set -e, for cd .. or cd /, or for popd -n.

If the directory is missing the script goes on in the old one, and the commands that follow (a rm -rf *, a build, a copy) run in the wrong place.

To fix, write cd dir || exit 1, or use set -e.

Reported
cd build
cd .. && make
cd /
cd build || exit
cd build && make
make && cd build
if cd build; then make; fi
while cd build; do break; done
cd build | cat
pushd build
popd -n
popd
if cd build | cat; then :; fi
{
cd build
}
f() { cd build; }
shell:
lint:
rules:
cd-without-exit: warn # off, warn (default), error

Enabled by the presets: shell/shellcheck.

Suppress one line with sudsy-disable-next-line cd-without-exit in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
shellcheck SC2164 full