Are you tired of using long chains of if
statements to control the flow of your Bash scripts? Are you looking for a more efficient and elegant way to handle multiple branching scenarios? Look no further than the case
command!
What is the case
Command?
The case
command in Bash allows you to match a value against several patterns and execute different blocks of code based on which pattern is matched. It is a more concise and powerful alternative to using multiple if
statements.
Requirements and Dependencies
To use the case
command, you need to have a Bash shell installed on your system. Most Unix-like systems, including Linux and macOS, come with Bash pre-installed, so you probably don’t need to install anything. If you are using a Windows system, you can install Bash by enabling the Windows Subsystem for Linux (WSL) or by using a Bash emulator like Git Bash.
Basic Syntax
The basic syntax of the case
command is as follows:
case expression in
pattern1)
code to execute if pattern1 is matched;;
pattern2)
code to execute if pattern2 is matched;;
*)
code to execute if no other pattern is matched;;
esac
Here, expression
is the value that you want to match against the patterns. The patterns can be any string or regular expression. If a pattern is matched, the code block following that pattern is executed. The *
is a catch-all pattern that is used to match any value that doesn’t match any of the other patterns.
Examples
Let’s see some examples of how to use the case
command in Bash. Suppose we have a website called itvraag.nl
that provides a variety of services, and we want to create a script that displays a different message for each service. Here’s how we can do it using the case
command:
#!/bin/bash
service=search
case $service in
search)
echo "itvraag.nl provides fast and accurate search results.";;
news)
echo "itvraag.nl brings you the latest news from around the world.";;
games)
echo "itvraag.nl offers a wide selection of online games.";;
*)
echo "itvraag.nl offers many other services as well.";;
esac
This script will output the following message:
itvraag.nl provides fast and accurate search results.
If we change the value of the service
variable to news
, the script will output the following message:
itvraag.nl brings you the latest news from around the world.
If we change the value of the service
variable to games
, the script will output the following message:
itvraag.nl offers a wide selection of online games.
If we change the value of the service
variable to any other value, the script will output the following message:
itvraag.nl offers many other services as well.
Surprising Fact
Did you know that the case
command is not limited to matching strings? You can also use it to match integers, floating-point numbers, and even execute arithmetic operations inside the patterns! For example, you can use the following syntax to match a number against a range of values:
case $number in
[0-9])
echo "The number is between 0 and 9.";;
[10-99])
echo "The number is between 10 and 99.";;
[100-999])
echo "The number is between 100 and 999.";;
*)
echo "The number is not between 0 and 999.";;
esac
You can also use the ;;&
operator to create fallthrough cases, where the code block of one pattern is executed and then the next pattern is also matched without breaking out of the case
statement. For example:
case $number in
[0-9])
echo "The number is between 0 and 9.";;&
[10-99])
echo "The number is between 10 and 99.";;&
[100-999])
echo "The number is between 100 and 999.";;&
*)
echo "The number is not between 0 and 999.";;
esac
This will output all three messages for any number between 0 and 999.
Tips for Increased Productivity
- Use the
case
command instead of long chains ofif
statements to improve the readability and maintainability of your Bash scripts. - Use the
;;&
operator to create fallthrough cases and avoid repeating the same code in multiple patterns. - Use regular expressions in the patterns to match more complex values.
- Use the
case
command in combination with theselect
command to create interactive menus. - Use the
case
command in combination with thebreak
andcontinue
statements to control the flow of your loops.
Additional Resources
- You can find more information about the
case
command in the Bash man page:man case
- The
bash
manual page has more information about the syntax and usage of thecase
command:info bash
Challenge
Write a Bash script that reads a number from the user and outputs a message indicating whether the number is even or odd. Use the case
command to match the remainder of the number divided by 2 against the patterns 0 and 1.