Bash case statements are similar to if-else statements but are easier and simpler. It helps to match one variable against several values. It is used when you require the IF-else statements with a number of Elif statements… It is quite similar to switch statement in C however bash case statement does not move forward once the pattern is matched

In this article, we will explain how to use the Bash case statement with example in Linux OS.

Syntax of bash case statement

Here is the syntax for the bash case statement:

Syntax:

case $variable in

pattern-1)

  commands

;;

pattern-2)

  commands

;;

pattern-3)

  commands

;;

pattern-N)

  commands

;;

*)

  commands

;;

esac

  • The case statement starts with “case” and ends with “esac”
  • The “)” is used to terminate a pattern. To separate multiple patterns, “|” operator is used as shown below:

case $variable in

     pattern-1| pattern-2)

        commands

        ….

        ….

        ;;

     pattern-3| pattern-4)

        commands

        ….

        ….

        ;;

  • Pattern with commands is known as clause and every clause ends with (;;).
  • The asterisk symbol* can be used to define the default case.
  • Bash case statement first matches the input $variable with the different patterns. If a pattern is matched, then the corresponding set of commands up to the double semicolons (;;) will be executed.

Example 1:

The following example is about the bash cash statement in which it asks the user to input the month name. If this input matches the defined patterns in the script, the corresponding echo command will be executed. The echo command will display information about the international event of that particular month.

To use this script, create a simple text file; name it with .sh file extension. Add the following script in it and save it. Then assign this file to execute permissions by running the following command in Terminal:

$ sudo chmod 777 filename.sh

To execute this bash file, simply run ./ followed by the bash file name as follows:

When you execute the script, you will be asked to enter the name of the month as input. If the month name is matched, it will display the corresponding event in that specific month, otherwise “No matching information found” will be displayed.

Note that “shopt -s nocasematch” is used to match pattern irrespective of its case.

shopt -s nocasematch

echo “Enter name of the month”

read month

case $month in

January)

echo ” 24th January international Day of Education.”

;;

February)

echo ” 20 FebruaryWorld Day of Social Justice .”

;;

March)

echo “8th March International women’s day.”

;;

April)

echo “7th April The World Health Day”

;;

May)

echo “The 15 May International Day of Families”

;;

June)

echo “20th June World Refugee Day”

;;

July)

echo “11th July World Population Day”

;;

*)

echo “No matching information found”

;;

esac

Example 2:

The following example is about the bash cash statement in which a user is asked to input the country name. If this input matches the defined patterns in the script, the corresponding echo command will be executed. The echo command will display information about the capital of that particular country.

To use this script, follow the same method described in the above example. Once done, execute the script, and you will be asked to enter the name of the country as input. If the country name is matched with the pre-defined country names, it will display the capital name of that country, otherwise, the “Information not available” message will be displayed.

#!/bin/bash

shopt -s nocasematch

echo -n “Enter the name of a country: “

read country

echo -n “The capital of $country is “

case $country in

Pakistan)

echo -n “Islamabad”

;;

Fiji)

echo -n “Suva”

;;

UK | “United Kingdom”)

echo -n “London”

;;

Turkey)

echo -n “Ankara”

;;

USA)

echo -n “Washington DC”

;;

*)

echo -n “Information not available”

;;

esac

echo “”

Bash case statements make the bash-script easier to read and understand. In this article, we have covered how to write and use bash case statements. Now you can write kind of multilevel if-else statement easily and simply by the use of case statements.

About the author

How to use the Bash case statement BASH Programming

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn.