Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 244
Hashbrown Avatar asked Jan 27 '26 07:01

Hashbrown


1 Answers

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
like image 54
KamilCuk Avatar answered Jan 30 '26 02:01

KamilCuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!