The case statement is used to choose which code to execute based on the value of a variable or expression. Here is how to use it in your Bash scripts.
What Does the case Statement Do?
The case statement is a convenient alternative to using multiple if/if else statements when you are deciding what action to take based on the value of a variable or expression.
For example, if you have a variable called weekday, you can executed different code based on which day of the week is named in the variable, allowing you to execute different code based on what day it is.
It’s just a simple, easy-to-read syntax for executing code conditionally, compared to using if statements.
Bash case Statement Syntax
The syntax for the case statement is as follows:
case EXPRESSION in CONDITION_1) # Code to execute for the given condition ;; CONDITION_2) # Code to execute for the given condition ;; CONDITION_N) # Code to execute for the given condition ;; *) # Code to execute for the wildcard/catch-all ;; esac
- EXPRESSION is the variable or expression that will be compared to the CONDITIONs
- The CONDITIONs are the values that the EXPRESSION should match to execute the code within that CONDITIONs code block
- Each condition is followed by a )
- If a condition is matched, the code for it is executed
- Each block of code for each CONDITION should end with a ;; to separate it from the next condition’s code
- If multiple conditions are matched only the FIRST match is executed
- Wildcard (*) conditions can be used as a catch all
- This is optional
- The entire case statement is wrapped in case/esac keywords
Bash case Examples
Here are some example case Bash scripts:
case Statements with Numerical Values
Consider the following if/*else statement:
#!/bin/bash echo -n "Enter number greater than 0: " read number if [ $number -lt 3 ]; then echo "$number is less than 3!" elif [ $number -eq 3]; then echo "$number is equal to 3!" else echo "$number is not less than 3!" fi echo "You entered $number"
This could be simplified into the following case statement:
#!/bin/bash echo -n "Enter number greater than 0: " read number case $number in [0-2]) echo "$number is less than 3!" ;; 3) echo "$number is equal to 3!" ;; *) echo "$number is not less than 3!" ;; esac
Note the use of a number range in the first condition to match a range of values for number, and the wildcard to catch any un-matched conditions.
This is easier to read, and requires less syntax, than the equivalent if/else statements.
Matching Strings with case
case statements are often used to control the flow of a script based on a string value from user input:
#!/bin/bash currentWeekday = date +%A case $currentWeekday in "Monday") echo "Boo, it's Monday" ;; "Saturday" | "Sunday") echo "It's the weekend!" ;; *) echo "It's one of those other work days" ;; esac
Above, a case statement is used to execute different code based on the current weekday (retrieved using the date program). Monday is matched to one code block, while Saturay OR Sunday is matched using the OR operator (|). The final wildcard will execute it’s code block for any day that does not match one of the prior conditions.