The CASE statement is a powerful conditional statement in Bash that allows you to test a variable against a list of values. It is a more concise and efficient alternative to using multiple if-then statements. Many times it is a good alternative to if-else statements. You can also use it for processing the command line arguments in a shell script.
In this article, we will introduce you to the CASE statement in Bash and provide examples of how it can be used in Bash scripts.
The syntax of the CASE statement in Bash
The syntax of the CASE statement in Bash is as follows:
1 2 3 4 5 6 7 8 9 10 | case expression in pattern1) commands1;; pattern2) commands2;; pattern3) commands3;; *) default_commands;; esac |
The case keyword indicates the start of the CASE statement, and the in keyword separates the expression to be tested from the list of patterns. Each pattern is enclosed in parentheses and followed by a list of commands to be executed if the pattern matches the expression. The double semicolon (;;)
indicates the end of each command list. The *)
pattern matches any expression that does not match any of the previous patterns, and the default_commands are executed in that case.
Using the CASE statement in Bash
The CASE statement in Bash is particularly useful when you want to compare a variable against a list of possible values. Here’s an example that shows how you can use the CASE statement to check the value of the $USER environment variable:
1 2 3 4 5 6 7 8 | case $USER in root) echo "You are logged in as root.";; ubuntu) echo "You are logged in as ubuntu.";; *) echo "You are logged in as an unknown user.";; esac |
In this example, the expression being tested is the value of the $USER environment variable. The patterns are the possible values of $USER (root and ubuntu), and the default pattern is the *)
pattern, which matches any value that is not root or ubuntu.
CASE Statement with Command Line Arguments
You can also use the CASE statement to process command line arguments in a Bash script. Here’s an example that shows how to use the CASE statement to handle different command line options:
1 2 3 4 5 6 7 8 9 10 11 12 | while getopts "a:b:c:" opt; do case $opt in a) echo "Option a was passed with argument $OPTARG";; b) echo "Option b was passed with argument $OPTARG";; c) echo "Option c was passed with argument $OPTARG";; *) echo "Invalid option: -$OPTARG";; esac done |
In this example, the getopts command is used to process the command line options. The case statement is used to handle each option (a, b, and c) and its corresponding argument. The default pattern (*)
is used to handle any invalid options.
CASE Statement with Regular Expression
The regular expressions used in the case statement are enclosed in square brackets ([])
. This is because we are using character classes to match different types of characters. The +
sign after the second pattern is used to match one or more occurrences of the previous pattern (in this case, lowercase letters). Regular expressions can be quite complex, and there are many more features and options that you can use in Bash to match patterns.
Here is an example of how you can use regular expressions with the case statement in Bash:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #!/bin/bash read -p "Enter a string: " str case $str in [a-z][0-9]) echo "The string starts with a lowercase letter and ends with a number." ;; [A-Z][a-z]+) echo "The string starts with an uppercase letter and is followed by lowercase letters." ;; [0-9]+) echo "The string contains only numbers." ;; *) echo "The string does not match any of the patterns." ;; esac |
In this example, we are using regular expressions to match different patterns of strings. The first pattern matches strings that start with a lowercase letter and end with a number, such as a1, b2, and so on. The second pattern matches strings that start with an uppercase letter and are followed by one or more lowercase letters, such as Hello, World, and so on. The third pattern matches strings that contain only numbers, such as 123, 4567, and so on. The default pattern matches any string that does not match any of the previous patterns.
When the script is run, it prompts the user to enter a string. The string is then tested against the different patterns using the case statement. If a pattern matches, the corresponding message is displayed. If none of the patterns match, the default message is displayed.
Conclusion
The CASE statement in Bash is a powerful and efficient alternative to using multiple if-then statements. It allows you to test a variable against a list of possible values and execute different commands based on the matching pattern. The CASE statement is particularly useful when you want to process command line arguments or compare a variable against a list of possible values. By using the CASE statement, you can write more concise and readable