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
Run in Warp
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.
This can be simplified with a case statement. As you can see, this is a lot more readable than the nested if-else statements.
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.
🧠 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.
Written by
Amit Jotwani
Developer Relations, Warp
Filed Under
Related Articles
Bash If Statement
Learn how to use the if statement in Bash to compare multiple values and expressions.
Bash While Loop
Learn how to use and control the while loop in Bash to repeat instructions, and read from the standard input, files, arrays, and more.
POST JSON Data With Curl
How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.
Use Cookies With cURL
Learn how to store and send cookies using files, hard-coded values, environment variables with cURL.
Loop Through Files in Directory in Bash
Learn how to iterate over files in a directory linearly and recursively using Bash and Python.
How To Use sudo su
A quick overview of using sudo su
Generate, Sign, and View a CSR With OpenSSL
Learn how to generate, self-sign, and verify certificate signing requests with `openssl`.
How to use sudo rm -rf safely
We'll help you understand its components
How to run chmod recursively
Using -R is probably not what you want
Run Bash Shell In Docker
Start an interactive shell in Docker container
Curl Post Request
Use cURL to send data to a server
Reading User Input
Via command line arguments and prompting users for input