Bash Case Statement | Explained

A case statement is a conditional statement often used in programming to check for multiple values of a variable or expression. The bash script case statement is similar to the switch case condition from other programming languages like C, C++, and JavaScript. Contrary to other programming languages, the bash case statement in Linux checks the true condition and quits the case statement loop.

This guide will discuss the bash case statement and its uses in Linux, using this timeline:

Let’s start the article by understanding the bash case statement.

What is a Bash Case Statement?

The Case statement is used in place of several nested if-else statements, which is easier to read and implement. It simplifies complex conditions when you have several matching outputs.

The Case statement checks every case to match it with the given input and outputs the matching value. The Case statement has the below general syntax:

case Expression in

Pattern-1)
Commands;; Pattern-2)
       Commands;;
Pattern-3)
       Commands;;

*)
       Commands;;

esac

Let’s understand the above Case Statement syntax:

  • case: Thisword shows the “case” statement is starting.
  • Expression: The case statement will match the expression with the case statements.
  • Pattern-1): It represents a specific clause, and multiple patterns can match the same set of commands using the OR (|) operator.
  • Commands: Replace it with the tasks you want to perform when a specific pattern is matched.
  • ;; : It shows a specific pattern is terminated here.
  • *): It shows the default pattern if no case expression is matched.
  • esac: It shows the case statement is closed here.

Two patterns can run the same command using the OR (|) operator, like if Pattern-1 and Pattern-2 output is similar while Pattern-3 and Pattern-4 have the same matching output, then use the following case statement:

case Expression in
Pattern-1) | Pattern-2)
Commands;;
Pattern-3) | Pattern-4
       Commands;;

*)
       Commands;;
esac

How to Use Bash Case Statements?

In this section, we will understand the bash case statement with the help of several examples.

Use Bash Case Statement to Do Specific Task

The basic use of the bash case command is to match the user input and show the matching result. In this example, we have 3 patterns (a, b, c) from where the output will show according to the user input. The read command takes the user’s input.

Write the below code in a bash file named “case_script.sh”:

#!/bin/bash

echo a=Check current date?
echo b=List files in current directory?
echo c=Find present working directory?
echo   echo "Hi! Choose your preferred choice?"
read choice

case $choice in
a)date;;
b)ls;;
c)pwd;;

*)echo "Invalid Choice"

esac

To execute the bash script, allow the execute permissions to the user running the following command:

$ chmod u+x ~/case_script.sh

Run the case script and choose the desired option to see the matching result:

$ ~/case_script.sh

Use Bash Case Statement to Check Number of Days in a Month

We can use the bash case statement with the OR (|) operator (executes the command if a single condition is matched) to check the number of days in a month by following these steps.

Write the below case statement code in a “case_script.sh” bash script file:

#!/bin/bash

echo "Enter Name of the Month?"
read month

case $month in

  February)
    echo "There are 28 days in $month"
    ;;

  April | June | September | November)
    echo "There are 30 days in $month"
    ;;

  January | March | May | July | August | October | December)
    echo "There are 31 days in $month"
    ;;

  *)
    echo "Invalid month: $month"
    ;;
esac

Allow the execute permissions with this command:

$ chmod u+x case_script.sh

Run the bash script file to check the output of the bash script file:

$ ~/case_script.sh

Use Bash Case Statement to Check File Extensions in a Directory

The case statement can be used within the for loop to check the type of every file extension in a specific directory. The for loop checks every file in the directory and the case statement matches it to the set cases.

To check file extension using case statement and for loop, use the code below in the bash script file: Note: The “ls” lists the files in the current working directory.

#!/bin/bash

for File in $(ls)
do
    File_Extension=${File##*.}
case "$File_Extension" in
txt)
echo "Text File: $File"
;;
mp4)
echo "mp4 File: $File"
;;
sh)
echo "Bash Script File: $File"
;;

*)
echo "Unknown File"
;;

esac
done

Change the case_script.sh file to executable using this command:

$ chmod u+x case_script.sh

Run the bash script to check the extension type of every file in that directory:

$ bash case_script.sh

Conclusion

The Bash case statement checks the multiple input values against the set cases. The case statement will only execute that ” true ” condition that matches the condition, and all other false conditions will be neglected. This bash guide has provided the detailed explanation of the bash case statements in Linux with the help of suitable examples.