9
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.

Hashbrown
  • 12,091
  • 8
  • 72
  • 95

1 Answers1

10

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
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • that's what I have (original pre-edit post had a typo) I need to fix the final statement, it should trigger for "everything ELSE", but is triggering for "everything" – Hashbrown Dec 10 '19 at 00:55
  • So just use `;;` ... It's designed to work that way... I don't understand, then why do you use a fallthrough if you don't want it to fallthrough? Please post some sample inputs and example outputs for the inputs. Maybe make the second `;;&` a `;;`? That way `*)` will not execute if `b` is matched. – KamilCuk Dec 10 '19 at 00:57
  • Then you lose the `a|b)` -> `b|c)`, the whole point of the question. It's specifically asking for the ability to use both, which other languages I've needed it in have – Hashbrown Dec 10 '19 at 00:58
  • I do want fallthrough, but I dont want fallthrough-`*)`, I want `default`. `*)` is a hack because bash doesnt have `default`, but they're the same thing when not using fallthrough. This question is: "but what if I am using fallthrough? What's my default-equivalent?" – Hashbrown Dec 10 '19 at 01:00
  • 1
    To account for your edit; if you only change the second `;;&` to `;;` that makes the final one mean "not b|c", which is not the same as "everything else" id est "not a|b|c". – Hashbrown Dec 10 '19 at 01:07
  • 1
    haha, we got the same idea in the end. I'll delete my answer since you did it first (I didn't see it), but I'll only add an upvote. I'm really looking for a default-equivalent – Hashbrown Dec 10 '19 at 01:10
  • 1
    for anyone else coming in, I'll quote my deleted answer; "But I'd appreciate a way not to re-list all the other options (so if I edit in more switches, I don't need to remember to add it to the list at the end)." – Hashbrown Dec 10 '19 at 01:11