bash case statements

When working with bash scripts, the Bash Case Statement will help to test simple values including integers and characters.

The Bash Case Statement is the simple form of the if-then-else statement in bash scripts. It will help you to match several values against one variable and It is very similar to the switch statement in C language and is more readable so anyone can understand it easily.

In this tutorial, we will show you how to use bash case statement with example in bash scripts.

Basic Syntax of Case Statements

The basic syntax of the bash case statement is shown below:

case $variable in
pattern-1)
commands
;;
pattern-2)
commands
;;
pattern-3)
commands
;;
pattern-N)
commands
;;
*)
commands
;;
esac

  • The case statement first checks the pattern and matches it against each pattern until a match is found, if the match is found, process the command-line until the double semicolon (;;).
  • *) acts as a default if no match is found.
  • The esac is similar to how fi closes an if statement that indicates the end of the case statement.

You can run the following command to displays the help information:

help case

You should see the following screen:

Case Statement Example

Let’s create a test.sh script using the case statement that will store number 4 in variable x and match it with different numbers, like 1, 2, 3, and 4, if both numbers are equal then print the message.

nano test.sh

Add the following lines:

#!/bin/bash
x=4
case "$x" in
"1") echo "x is equal to 1" ;;
"2") echo "x is equal to 2" ;;
"3") echo "x is equal to 3" ;;
"4") echo "x is equal to 4" ;;
*) echo "x is greater than 4" ;;
esac

Save and close the file when you are finished. Then, run the script as shown below:

bash test.sh

You should see the following output:

x is equal to 4

Next, create a new test.sh script using the case statement that will ask you to enter your favorite website name and tell you either it is famous of not.

If any condition does not match it will print the message “I don’t know”

nano test.sh

Add the following lines:

#!/bin/bash
echo -n "Enter your famous website name: "
read WNAME
case $WNAME in
Webservertalk)
echo -n "Yes, it is famous website."
;;
Netadmintools)
echo -n "Yes, it is famous website."
;;
Tecadmin)
echo -n "No, its not famous website."
;;
*)
echo -n "I don't know."
;;
esac

Save and close the file when you are finished. Then, run the script as shown below:

bash test.sh

You will be asked to provide your famous website name. For example, if you type “Webservertalk” it will match the first pattern and it will print the following message:

Enter your famous website name: Webservertalk
Yes, it is famous website.

If you type “Tecadmin” it will match the third pattern and it will print the following message:

Enter your famous website name: Tecadmin
No, its not famous website.

If you type the “Linux” it will not match any pattern and print the following message:

Enter your famous website name: Linux
I don't know.

Conclusion

Thats it for now. I hope you have learned enough about how to use Bash Case Statements in the bash script to assist you with any issues or problems your trying to solve.

Feel free to comment below if you have any questions.