I’ve known case allows the script to fall through cases since Bash 4, but never really got a chance to use that. I was thinking about trying friendly interactive shell (fish), while reading its tutorial, the mention of fall-through reminded me of the case fall-through, although fish doesn’t seem to have that.

Anyway, considering the following script:

case $1 in
  5)
    echo -n e
    ;;
  4)
    echo -n d
    ;&
  3)
    echo -n c
    ;;&
  1|2|5)
    echo -n b
    ;;&
  1|3|4)
    echo -n a
    ;;
esac
echo

And the following inputs and outputs:

$ ./case.sh 5
e
$ ./case.sh 4
dca
$ ./case.sh 3
ca
$ ./case.sh 2
b
$ ./case.sh 1
ba

The most used ;; will end the testing right away, that’s why input 5 isn’t tested with the second pattern from bottom.

;& will fall through to next case without testing the pattern. Input 4 falls through pattern 3‘s case, the ;;& will fall through if input 4 matches pattern 1|2|5. Obviously it doesn’t match so it goes on next case, pattern 1|3|4 which is matched.

Basically, when one of the following is seen:

  • ;; ends case.
  • ;& falls through next case without testing.
  • ;;& tests next case, if matches, run the case; if not, keep testing the remaining cases.