Bash Case Statement

Last Updated 2022-09-06

A case statement is a conditional control structure that allows a selection to be made between several sets of program statements. It is a popular alternative to the if-then-else statement when you need to evaluate multiple different choices.

Case Statement Syntax


  case expression in
    pattern1 )
      statements ;;
    pattern2 )
      statements ;;
    ...
  esac

To create a case statement:

  1. Start with the word case, and follow it with a variable or an expression you’re trying to match, and end the line with the word in.
  2. Next, list a pattern or value you want to match against the expression or variable in the case statement. End the pattern a with a parenthesis.
  3. Finally, insert the commands you’d like to be executed if the pattern is matched. Be sure to add a double semicolon ;; when you’d like the execution to stop.

Now when the case statement is run, the commands following the first pattern match will be executed. The execution will stop when a double semicolon is reached, and the script will exit the case statement.

When to use If-else vs a Case statement

Much like the if-then-else statement, the bash case statement is a conditional statement used to vary the flow of your shell script.

While the if-else statement is used to choose between two options, the case statement is helpful when you have multiple different choices. Case statements are a lot easier to read and maintain compared to nested if-then-else statements you may otherwise use.

In fact, a good rule of thumb is if you find yourself using an if statement to compare the same variable or expression against different values or patterns, it’s probably a good idea to use a case statement instead.

Here’s an example of a somewhat complex if-else statement with multiple nested if-else statements.

if-else statement with nested if-else statements

This can be simplified with a case statement. As you can see, this is a lot more readable than the nested if-else statements.

A case statement that is much more readable

One thing to note is that the case statement stops searching for a pattern match as soon as it finds one. It's not a loop, as in it does not execute a block of code for n times. It stops at the first instance of the pattern match it's looking for.

💡Tip from Warp

Also, it’s a good practice to use the wildcard asterisk symbol (*) as a final pattern to define the default case. This pattern will always match, and it tells the interpreter that if no other pattern matches, default to this match.

Using a wildcard (*) as a final pattern

🧠 Did you know?

If you’re curious about the peculiar syntax of the case statement in bash, you’re not alone! Here’s this Stack Overflow question answering it.

TL;DR:

The Bash syntax came from Bourne (of Bourne shell fame). He had worked on Algol, and liked it enough to model some of the shell syntax on Algol. Algol uses reversed keywords to mark the ends of constructs, so 'case ... esac' was appropriate. The reason that loops do not end with 'od' is that there was already a command 'od' in Unix - octal dump. So, 'done' is used instead.