Introduction
The bash case
statement is generally used to simplify complex conditionals when there are multiple different choices. Using the case
statement instead of nested if
statements will help make your bash scripts more readable and easier to maintain.
The Bash statement has a similar concept to the case
statement in Javascript or switch
in C. The main difference is that, unlike C's switch
statement, the Bash case
statement does not continue to look for a matching pattern once it has one. found one and executed the instructions associated with that pattern.
This tutorial will cover the basics of the Bash case
statement and show you how to use it in your shell scripts.
Case statement syntax
The Bash case statement syntax consists of the case
keyword " " followed by the value to match, the in
" ", and one or more patterns with the corresponding code blocks enclosed in ;;
instructions " ":
case EXPRESSION in
PATTERN_1)
STATEMENTS
;;
PATTERN_2)
STATEMENTS
;;
PATTERN_N)
STATEMENTS
;;
*)
STATEMENTS
;;
esac
- Each
case
statement begins with thecase
keyword, followed by the case-sensitive expression and thein
keyword. The statement ends with the keywordesac
. - You can use multiple templates separated by the
|
operator. The)
operator terminates a list of patterns. - A pattern can contain special characters.
- A template and its associated commands are known as a clause.
- Each clause must end with
;;
. - The commands corresponding to the first pattern that matches the expression are executed.
- It is common practice to use the wildcard asterisk symbol (
*
) as the final pattern to define the default case. This pattern will always match. - If no pattern is found, the returned status is zero. Otherwise the return state is the exit state of the executed commands.
Case statement example
Below is an example of using a case
statement in a bash script that prints the official language of a particular country:
#!/bin/bash
echo -n "Enter the name of a country: "
read COUNTRY
echo -n "The official language of $COUNTRY is "
case $COUNTRY in
Lithuania)
echo -n "Lithuanian"
;;
Romania | Moldova)
echo -n "Romanian"
;;
Italy | "San Marino" | Switzerland | "Vatican City")
echo -n "Italian"
;;
*)
echo -n "unknown"
;;
esac
Save the custom script as a file and run it from the command line.
bash languages.sh
The script will ask you to enter a country. For example, if you type "Lithuania", it will match the first pattern and the echo
command in that clause will be executed.
The script will print the following output:
Enter the name of a country: Lithuania
The official language of Lithuania is Lithuanian
If you enter a country that doesn't match any other pattern except the default wildcard asterisk symbol, say Argentina, the script will execute the echo
command within the default clause.
Enter the name of a country: Argentina
The official language of Argentina is unknown
Conclusion
At this point you should have a good understanding of how to write bash case
statements. They are often used to pass parameters to a shell script from the command line. For example, init scripts use case
statements to start, stop, or restart services.