How to Use the Bash Case Statement
The case statement is generally used in Bash when you have multiple choices to select from. Using it instead of nested if-else statements helps keep your Bash scripts easier to read and maintain.
In this guide, we will explain how you can use the Bash case statement using two examples and some scenarios in Ubuntu 22.04.
Syntax of the Case Statement
If you want to understand how to use the case statement, you need to understand the syntax. It looks like this:
Let’s break it down:
- It’s imperative that you open up the statement with the “case” keyword and close it out with the “esac” keyword.
- We use the closing bracket “)” to terminate a pattern.
- Patterns containing commands are known as clauses and they end with “;;”.
- You can use an “*” (asterisk) to define a default case.
- When the case command matches the input variable with the defined patterns, upon a match, the corresponding set of commands code are executed up to the double semicolons.
- If you want the same command to be executed for multiple patterns, you can use the pipe operator to put the patterns together as such:
Now, let’s take a look at a couple of examples to understand this syntax better.
Holidays in a Month
For our first example, let’s write a script that takes a month as input from the user and responds with an international holiday that falls in the month. The “shopt -s nocasematch” command allows your script to ignore the case of the input and match it with the cases irrespective of that.
In this script, we take the name of a month from the user and store it in a variable named “month”. Then, using the case statement, we compare the input with some pre-selected months and then display the respective holiday. You can see that if the code doesn’t find any match in the defined cases, it then have an echo outside the cases to display another message. And after that, we close the case code structure with the “esac” keyword.
Save the script file and exit out of the nano editor. Run the script using the following command:
You can see that the code responded with the relevant holiday for the input that was catered to with cases, and responded with unknown when the input was out of range from the programmed cases.
The Official Language of a Country
Let’s write a script that utilizes the composite cases. For this example, let’s write a script that takes a country and prints the official language of that country.
Case Statement with Integers
You can also create the cases where you have to match the input with integers as well. Let’s create a script that takes the input from the users in the range of 1-10, and then displays if the number is even or odd.
Running this script gives you the following output:
You can test with the other numbers as well.
Case Statement with For Loop
You can use the case statement within the for-loops if you want to check multiple input values against your set cases. Let’s illustrate this case by writing a script that goes through all of the files in the current directory and display their file type.
Running this script goes through every file in the current active directory and displays it along with its file type (only the cases we specified). As you can see, when running this script that we only have one file in the current directory, it displays that along with the extension.
Going back to your older code and figuring out what line does can be a very cumbersome task, especially if it was written by somebody else. Adding to it is even harder. Using the case statement in your Bash scripts can make it very easy for you to keep it readable through the easy syntax.
As demonstrated with the examples, you can use it with strings, multiple strings, integers, and for loops, among many others. You can even handle the exit codes for your program with case statements. It’s very versatile.
If you face any issues using the case statement, feel free to engage us and we’ll gladly help you out.