Home Shell Scripting How to Work with Case Statement in Bash Scripts

How to Work with Case Statement in Bash Scripts

In this article, we will take a look at bash case statements and how to use them in bash scripts. The case statement is a bash built and is used to simplify conditional logic in bash scripts.

[ You might also like: How to Use Declare Command in Linux Bash Shell ]

To put it in simple terms, when your code has multiple conditional [ if - elif - else ] statements you can replace the conditional statements with case statements. The concept of the case statement is also available in other programming languages like C, Perl, Java, Javascript, etc. In some languages, it will be a switch case statement and in some languages, it will be a case statement, it differs according to the language you use.

Understanding Syntax for the Case Statement

Before using the case statement in your scripts you have to understand its syntax.

case expression in

  pattern1)
    STATEMENTS ;;

  pattern2 | pattern3)
    STATEMENTS ;;

  pattern-N)
    STATEMENTS ;;
    
  *)
    STATEMENTS
    ;;
esac

Above case statement explanation.

  • The case statement should start with the case keyword and end with the esac keyword.
  • The case statement will evaluate an expression and find if the expression is matching any of the patterns. The expression should come after the case keyword.
  • The patterns will be evaluated and if it matches the expression then the statement will be executed. Patterns and statements are called clauses. To terminate a clause use a double semicolon ";;".
  • If a pattern is matched then the rest of the patterns are skipped.
  • You can create as many patterns as you want.
  • The asterisk symbol (*) will be used as the default pattern. If an expression is not matched with any of the patterns then the default pattern will be used.

Creating a Simple Case Statement in Script

I am creating a simple case statement that will display the team that won the champions league based upon year. The user has to pass the year as the input.

#!/usr/bin/env bash

read -p "Enter the year to get the winner name : " WINNER
case $WINNER in
  2016) echo "  Winner is  Real Madrid";;
  2017) echo "  Winner is  Real Madrid";;
  2018) echo "  Winner is  Liverpool";;
  2019) echo "  Winner is  Bayern Munich";;
  2020) echo "  Winner is  Chelsea";;
  *) echo " No data available for the given option";;
esac
Case Statement Example
Case Statement Example

Take a look at the above code and output. User is passing year as the input and stored in variable WINNER. The year will be evaluated against all the years given as patterns inside the case statement. If the year passed by the user is not matching any pattern default pattern (*) is used.

It is also possible to evaluate multiple patterns in a single clause. You have to use the "|" symbol for using one or more patterns in a clause. We will use the same code from the previous example. If you take a look at the years 2016 and 2017 same team won the cup. Instead of creating separate clauses for 2016 and 2017, you can create a single clause.

#!/usr/bin/env bash

read -p "Enter the year to get the winner name : " WINNER
case $WINNER in
  2016 | 2017) echo "  Winner is  Real Madrid";;
  2018) echo "  Winner is  Liverpool";;
  2019) echo "  Winner is  Bayern Munich";;
  2020) echo "  Winner is  Chelsea";;
  *) echo " No data available for the given option";;
esac

Now when the user passes 2016 or 2017 as the input it will be evaluated in the first clause.

Case Statement Example - Multiple Patterns
Case Statement Example – Multiple Patterns

You can also use Pattern matching in the case statements. I am again modifying the same code and adding two more clauses to evaluate for strings and other years.

#!/usr/bin/env bash

read -p "Enter the year to get the winner name : " WINNER
case $WINNER in
  2016 | 2017) echo "  Winner is  Real Madrid";;
  2018) echo "  Winner is  Liverpool";;
  2019) echo "  Winner is  Bayern Munich";;
  2020) echo "  Winner is  Chelsea";;
  [a..z]* | [A..Z]*) echo " Year should be passed as argument. Input is wrong";;
  [0-9][0-9][0-9][0-9]) echo "No data for the passed year..."
esac
Case Statement Example Pattern Matching
Case Statement Example Pattern Matching

I suggest you take a look at ~/.bashrc file and files under /etc/init.d/ where the case statement will be used. Going through these files will give you some idea of how it is used in real-time.

That’s it for this article. The case statement is an important concept to know when you work with shell scripts. When you start replacing conditional statements with case statements your code will be clean and maintainable.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

2 thoughts on “How to Work with Case Statement in Bash Scripts”

  1. Thank you so much for putting this article together. Simple yet effective. It legitimately helped me understand the case statement in bash scripting, whereas I was having a bit of difficulty in understanding it as part of the online course I’m doing.

    Reply

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.