Bash Case Statement

Introduction

When you have several options, the case statement in Bash is commonly used to reduce complex conditionals. You may make your bash scripts more legible and maintainable by using the case statement instead of nested if statements.

The Bash case statement is similar to the Javascript or C switch statement in terms of principle. The primary distinction is that, unlike the C switch statement, the Bash case statement does not keep looking for a pattern match after it finds one and executes the sentences associated with it.

In this tutorial, we'll go over the basics of the Bash case statement and how to utilize it in shell scripts. We will also address a few FAQs on Bash Case Statement.

case Statement Syntax

The case statement in Bash looks like this:

case EXPRESSION in

  PATTERN_1)
    STATEMENTS
    ;;

  PATTERN_2)
    STATEMENTS
    ;;

  PATTERN_N)
    STATEMENTS
    ;;

  *)
    STATEMENTS
    ;;
esac
  • The case keyword is followed by the case expression and the in keyword in each case statement. The esac keyword completes the statement.
  • The | operator can be used to separate several patterns. A pattern list is terminated with the ) operator.
  • Special characters can be used in a pattern.
  • A clause is a combination of a pattern and its accompanying commands.
  • ;; must be used to end each clause.
  • The commands associated with the first pattern that matches the expression are carried out.
  • The wildcard asterisk sign (*) is commonly used as the last pattern to establish the default scenario. This pattern will always be consistent.
  • The return status is zero if no pattern is found. Otherwise, the exit status of the performed commands is the return status.

Case Statement Example

Here's an example of a Bash script that uses the case statement to output the official language of a particular country:

#!/bin/bash

echo -n "Enter the name of a country: "
read COUNTRY

echo -n "The official language of $COUNTRY is "

case $COUNTRY in

  Lithuania)
    echo -n "Lithuanian"
    ;;

  Romania | Moldova)
    echo -n "Romanian"
    ;;

  Italy | "San Marino" | Switzerland | "Vatican City")
    echo -n "Italian"
    ;;

  *)
    echo -n "unknown"
    ;;
esac

Create a file with the custom script and run it from the command line.

bash languages.sh

You will be prompted to select a country. If you type "Lithuania," for example, the first pattern will be matched, and the echo command in that clause will be executed.

The following output will be printed by the script:

Output

Enter the name of a country: Lithuania
The official language of Lithuania is Lithuanian

If you type Argentina as a country that doesn't match any other pattern save the default wildcard asterisk symbol, the script will run the echo command inside the default clause.

Output

Enter the name of a country: Argentina
The official language of Argentina is unknown

FAQs on Bash Case Statement

Can I use regular expressions in the Bash case statement? 

No, the Bash case statement does not support regular expressions. It only supports simple wildcard patterns like *, ?, and character classes [ ].

How do I match patterns in the case statement? 

The patterns in the case statement are checked sequentially until a match is found. The matching pattern's code block is executed, and the case statement exits. If no match is found, the *) pattern (default) is used.

Can I use variables in the patterns of a case statement? 

Yes, you can use variables in the patterns. They will be expanded similar to other variable expansions in Bash. For example: pattern="abc"; case "$var" in $pattern) echo "Match";; esac.

How can I match multiple patterns in the same code block? 

You can group multiple patterns together by separating them with the pipe symbol |. For example: case "$var" in pattern1|pattern2) echo "Match";; esac.

Is there a difference between * and ? in the case statement? 

Yes, there is a difference. The * matches any number (including zero) of characters, while ? matches exactly one character.

How can I perform case-insensitive matching in the case statement? 

In Bash, matching is case-sensitive by default. To perform a case-insensitive match, you can convert the given variable or pattern to lowercase using the tolower function. For example: case "$(tolower $var)" in pattern) echo "Match";; esac.

Can I use the case statement with numbers? 

Yes, the case statement can be used with numbers. You can either perform numeric comparisons directly or use pattern matching to match specific ranges or values.

Conclusion

You should have a good idea of how to write case statements in Bash by now. They're frequently used to give command-line parameters to a shell script. Case statements are used in the init scripts to start, halt, and restart services.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.