Does bash have a `default`-equivalent for the case statement when using fallthrough?

11.3k Views Asked by At
case "$action" in
a|b)
    echo for a or b
;;&
b|c)
    echo for c or b
;;&
*)
    echo for everything ELSE
;;&
esac

So, as you can see, I'm using ;;& instead of ;; so that if action=b it will trigger both of the first two cases.
However, a drawback of this is that *) no longer 'means' "everything else", but will match "everything" instead; thus b will trigger the final one also.

PowerShell is able to do what I want because it has a dedicated default keyword, does Bash have something similar?
What about an exhaustive work-around like [!(a|b|c)]) or something?

It would have been handy to do something like case 5 in 4) echo bob; ;;& esac || echo DEFAULT but case doesn't seem to return any code.

1

There are 1 best solutions below

7
On

From bash manual:

If the ‘;;’ operator is used, no subsequent matches are attempted after the first pattern match. Using ‘;&’ in place of ‘;;’ causes execution to continue with the command-list associated with the next clause, if any. Using ‘;;&’ in place of ‘;;’ causes the shell to test the patterns in the next clause, if any, and execute any associated command-list on a successful match, continuing the case statement execution as if the pattern list had not matched.

Maybe such idea:

case "$action" in
a|b)
    echo for a or b
    ;;&
b|c)
    echo for c or b
    ;;&
a|b|c) ;;
*)
    echo for everything ELSE
esac