Bash Case Statement
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
To create a case statement:
- 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.
- 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.
- 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.
Related articles
Bash Comments
Comments will help make your scripts more readable
Reading User Input
Via command line arguments and prompting users for input
Curl Post Request
Use cURL to send data to a server
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.
Upload Files With curl
Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.
How To Copy A Directory In Linux
Learn how to copy directories and their content in Linux using the cp command with options like -r for recursive copying, -i for interactive mode, and -a for preserving attributes.
Create Groups In Linux
Learn how to manually and automatically create and list groups in Linux.
How to Check the Size of Folders in Linux
Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.
Count Files in Linux
Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.
List Open Ports in Linux
Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.
Format Command Output In Linux
Learn how to filter and format the content of files and the output of commands in Linux using the awk command.