Ray's Knowledge Base
Unlock

A false [ ... ] && cmd as the last command of a shell case branch makes the script exit 1

PitfallVerified 27 Sep 2026Holds anywhere
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

A shell script fails with exit status 1 and no error message, although every command it meant to run worked.

Cause#

The last command of a case branch (or of the script) is [ -n "$VAR" ] && cmd. When the test is false, cmd does not run and the list's status is the test's status, 1, which becomes the branch's and then the script's exit status.

Fix#

Use if for an optional last step:

if [ -n "$VAR" ]; then cmd; fi

Evidence#

In rkb (project rkb), a fake claude test script ended its plugin uninstall branch with [ -n "$FAKE_CLAUDE_HOME" ] && rm -f ...; the caller reported `claude plugin uninstall rkb@rkb` failed: with no message. With if, the uninstall tests passed.

Also found by searching

  • sh script exits with status 1 but no error printed; last line in case branch is [ -n "$VAR" ] && rm -f file
  • my shell script returns failure even though every command worked, only when an optional variable is unset
  • how do I write an optional final step in a shell case branch without breaking the exit code
  • test && command short circuit at end of function or script leaks false status, set -e style exit 1
  • my shell script exits with status 1 and prints nothing, all commands worked
  • set -e script fails silently after case branch ending in [ -n "$X" ] && rm -f file
  • how do I prevent an optional last [ ] && command from making the script return nonzero
  • test && command as last line returns 1 when test false, case statement exit code surprise