223

I am looking for the correct syntax of the switch statement with fallthrough cases in Bash (ideally case-insensitive). In PHP I would program it like:

switch($c) {
    case 1:
        do_this();
        break;
     case 2:
     case 3:
        do_what_you_are_supposed_to_do();
        break;
     default:
        do_nothing(); 
}

I want the same in Bash:

case "$C" in
    "1")
        do_this()
        ;;
    "2")
    "3")
        do_what_you_are_supposed_to_do()
        ;;
    *)
        do_nothing();
        ;; 
esac

This somehow doesn't work: function do_what_you_are_supposed_to_do() should be fired when $C is 2 OR 3.

3
  • 5
    Don't use call functions with parens!!! Since you can define a function in bash using either function fname { echo "Inside fname"; return 0; } or fname() { echo "inside fname"; return 0; } placing parens on a function call can look like it's a function defintion. Functions should be called like any other command line program such as mv, cp, rsync, ls, cd, etc... In this case we call fname like so: fname $ARGS. Dec 17, 2015 at 17:36
  • 1
    do_nothing() shall be a SKIP statement? Use :.
    – sjas
    Jul 21, 2016 at 12:01

5 Answers 5

355

Use a vertical bar (|) for "or".

case "$C" in
"1")
    do_this()
    ;;
"2" | "3")
    do_what_you_are_supposed_to_do()
    ;;
*)
    do_nothing()
    ;;
esac

Bash Reference Manual: Conditional Constructs. case

3
  • 34
    Or in this simple case a character class [23])
    – SiegeX
    Apr 6, 2011 at 6:50
  • 5
    @Mischka - I note you haven't accepted this answer, is that because it doesn't answer the fallthrough part of the question? Fallthrough logic is useful where special processing should occur before do_what_you_are_supposed_to_do(), collapsing both "2" and "3" into a single case fails to address this. I'm unsure if editing the question to make this clearer is reasonable, since it's obvious that many people have found this answer helpful.
    – Tyson
    Apr 23, 2018 at 4:03
  • 2
    @Tyson The OP hasn't accepted the answer, since it is an unregistered user. As to the "fall-through" logic, as normally understood by programmers, the question body demonstrates a collapsing of condition, not fall-through logic. (Notice the use of break in the php code.) Editing the question, or its title, at this date would invalidate many of the answers which do provide fall-through logic, and probably shouldn't be done. Fall-through wasn't in the title until several edits by other users, but it's too late to take it back out now.
    – Chindraba
    Feb 8, 2019 at 5:30
121

Recent bash versions allow fall-through by using ;& in stead of ;;: they also allow resuming the case checks by using ;;& there.

for n in 4 14 24 34
do
  echo -n "$n = "
  case "$n" in
   3? )
     echo -n thirty-
     ;;&   #resume (to find ?4 later )
   "24" )
     echo -n twenty-
     ;&   #fallthru
   "4" | [13]4)
     echo -n four 
     ;;&  # resume ( to find teen where needed )
   "14" )
     echo -n teen
  esac
  echo 
done

sample output

4 = four
14 = fourteen
24 = twenty-four
34 = thirty-four
8
  • 2
    This logic in this example was difficult to follow. Also, I don't think this example really demonstrates the difference between ;& and ;;&. I changed the "24" to ;;& # resume and got the same results, so I'm still wondering when you would use ;& fallthrough.
    – wisbucky
    Feb 7, 2020 at 0:53
  • 2
    it's a simple example for a complex thing - I changed ?4 to [13]4 to make it more obvious, and to make your change a breaking change
    – Jasen
    Feb 7, 2020 at 1:02
  • Unfortunately not supported on macos' bash :(
    – v01pe
    Oct 16, 2020 at 15:35
  • @JerryGreen well what can I say, here's my output: test.sh: line 7: syntax error near unexpected token `&' test.sh: line 7: ` ;;& #resume (to find ?4 later )' Here's my bash --version: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin19)
    – v01pe
    Nov 8, 2020 at 14:11
  • 1
    @v01pe OH SHIT SORRY. I'm switching like 10 times per day between macos and windows (with linux-like MINGW64 shell), and actually I didn't even realize that I used this script on windows LOL. Just tried it on my macbook with the same exact (preinstalled) bash version: I confirm, it doesn't work :\ On windows though it shows GNU bash, version 4.4.23(1)-release (x86_64-pc-msys), so it seems it's a feature of newer Bash version :\ Thx for the notice btw! (I deleted my previous comment due to being misinfo) Nov 9, 2020 at 5:40
30
  • Do not use () behind function names in bash unless you like to define them.
  • use [23] in case to match 2 or 3
  • static string cases should be enclosed by '' instead of ""

If enclosed in "", the interpreter (needlessly) tries to expand possible variables in the value before matching.

case "$C" in
'1')
    do_this
    ;;
[23])
    do_what_you_are_supposed_to_do
    ;;
*)
    do_nothing
    ;;
esac

For case insensitive matching, you can use character classes (like [23]):

case "$C" in

# will match C='Abra' and C='abra'
[Aa]'bra')
    do_mysterious_things
    ;;

# will match all letter cases at any char like `abra`, `ABRA` or `AbRa`
[Aa][Bb][Rr][Aa])
    do_wild_mysterious_things
    ;;

esac

But abra didn't hit anytime because it will be matched by the first case.

If needed, you can omit ;; in the first case to continue testing for matches in following cases too. (;; jumps to esac)

3
  • You can also convert C to lowercase case "${C,,}" in if the case isn't important Mar 22, 2014 at 12:02
  • 1
    What happens if you do omit the ;;? Aren't you supposed to use ;& for fallthrough? Jan 22, 2016 at 16:21
  • 1
    I get a syntax error if I omit ;; I need to use ;;& if I want to continue testing.
    – Jasen
    Oct 27, 2016 at 20:53
18

Try this:

case $VAR in
normal)
    echo "This doesn't do fallthrough"
    ;;
special)
    echo -n "This does "
    ;&
fallthrough)
    echo "fall-through"
    ;;
esac
2
  • 1
    that is a very helpful tip. At least for Bash 4.3.11. I haven't bothered to try it on any others.
    – Daniel
    Dec 6, 2015 at 4:50
  • 3
    Note: This doesn't work for bash 3.2, which is still the default bash in macOS. Oct 26, 2016 at 16:44
15

If the values are integer then you can use [2-3] or you can use [5,7,8] for non continuous values.

#!/bin/bash
while [ $# -gt 0 ];
do
    case $1 in
    1)
        echo "one"
        ;;
    [2-3])
        echo "two or three"
        ;;
    [4-6])
        echo "four to six"
        ;;
    [7,9])
        echo "seven or nine"
        ;;
    *)
        echo "others"
        ;;
    esac
    shift
done

If the values are string then you can use |.

#!/bin/bash
while [ $# -gt 0 ];
do
    case $1 in
    "one")
        echo "one"
        ;;
    "two" | "three")
        echo "two or three"
        ;;
    *)
        echo "others"
        ;;
    esac
    shift
done
2
  • what is the shift at the end for? Nov 10, 2019 at 13:43
  • 2
    shift removes first argument on CLI arguments list. Basically on every iteration of this loop always $1 is used to get each argument from CLI arguments list with the help of shift.
    – rashok
    Nov 10, 2019 at 14:25

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.