In many other programming languages, this is called the switch case statement. In Bash, the Case statement is one of the decision making statements that let us execute a block of code.
The Bash case statement can be taken as a nice and easily readable solution to the nested-if statements.
In the case statement, you may test several values for one variable. For example, we ask the user to enter a color code. Upon entering different colors, we execute a block of code depending on the entered color. The entered value can be stored in a variable ‘$color’ and its value can be tested by using the case statement.
If black is entered, you can execute the block of code related to black color. Similarly, red, green, and white etc. The examples in the coming section should make things clearer.
Syntax for using the case statement
Following is the general way of using the case statement in Bash:
case expression in pattern1) stataments to be executed ;; pattern2) stataments to be executed ;; pattern3) stataments to be executed ;; *) Default condition to be executed ;; esac
Points to be noted in the syntax:
- First of all, the “case” keyword is given which is followed by an expression and “in” keyword.
- The interpreter checks the value of an expression against each case and if a match is found, it executes the given statement(s) in that case.
- The double semicolon (;;) is like the break statement in C and other languages. After a match is found and its statements are executed, the ;; makes exit the case statement.
- If no match is found, the default condition can be provided. In that case, you may execute the default statement if none of the cases is true.
- Finally, the “esac” keyword is given to end the case statement.
A simple example of case statement
In this example, we have a variable color which value is tested in the case statement. For five different colors, the respective message is displayed:
Color="yellow" case "$Color" in "black") echo "The color is Black." ;; "green") echo "The color is Green." ;; "yellow") echo "The color is Yellow." ;; "blue") echo "The color is Blue." ;; "brown") echo "The color is Brown." ;; esac
The output:
The color is Yellow.
You can see, we set the value of Color variable “yellow” initially. The third pattern is matched and so its statement executed.
The example of case statement with multiple values for each match
In the above example, we provided only one value in each pattern and displayed the message accordingly.
In many cases, you may require using the multiple values of each match and execute the same statements if any of the matches are found.
The example below shows how I used States/Cities in each match and then displayed the country name as per the value of the variable. Have a look:
place="Frankfurt" case $place in "New York" | Texas | Florida ) echo "The Country is United States!" ;; London | Manchester | Birmingham ) echo "The Country is United Kingdom!" ;; Berlin | Munich | Hamburg | Frankfurt) echo "The Country is Germany!" ;; Paris| Lyon| Toulouse | Strasbourg) echo "The Country is France!" ;; Riyadh| Jeddah| Mecca | Medina) echo "The Country is KSA!" ;; Mumbai| Delhi | Bangalore | Hyderabad) echo "The Country is India!" ;; Karachi| Lahore | Faisalabad | "Dera Ghazi Khan") echo "The Country is Pakistan!" ;; *) echo "To be added soon!" ;; esac
The output of the above script is:
The Country is Germany!
You can see, multiple values are separated by a pipe sign i.e. |
You may also notice that the values with spaces are enclosed in double-quotes while single word values are given without quotes.
Case insensitive matching example
In the above example, if we assigned the “frankfurt” rather than “Frankfurt” value to the variable, the result would have been the default case i.e.
“To be added soon!”
By default, the string value is case-sensitive as the case statement matches the pattern. However, if you are taking the user input then you may expect a user entering the values without considering the case.
So, how to make it case-insensitive? This is easy to do.
For that, you may set the nocasematch shell option. The example below shows how:
shopt -s nocasematch day="wED" case $day in Sat ) echo "The day is Saturday!" ;; Sun ) echo "The day is Sunday!" ;; Mon ) echo "The day is Monday!" ;; Tue) echo "The day is Tuesday!" ;; Wed) echo "The day is Wednesday!" ;; Thu) echo "The day is Thursday!" ;; Fri) echo "The day is Friday!" ;; *) esac
The output:
The day is Wednesday!
You see, the variable assigned the “wED” and still, the “Wed” pattern is matched and its statement is executed.