Bash Scripting – Case Statement

A case statement in bash scripts is used when a decision has to be made against multiple choices. In other words, it is useful when an expression has the possibility to have multiple values. This methodology can be seen as a replacement for multiple if-statements in a script. Case statements have an edge over if-statements because it improves the readability of our code and they are easier to maintain. Case statements in a Bash script are quite similar to Case statements in C language. But unlike C, the Bash Case statement stops continuing the search as soon as the match occurs. In simple words, they don’t require any break statement that is mandatory to be used in C to stop searching for a pattern further.

The basic syntax of a case statement is given below,

Syntax:

case EXPRESSION in Pattern_Case_1) STATEMENTS ;; Pattern_Case_1) STATEMENTS ;; Pattern_Case_N) STATEMENTS ;; *) STATEMENTS ;; esac 

Here’s how the case statement works:

Example 1: Output a Description for Each Option

In this example a script allows the user to select a department and shows a description corresponding to the input with the help of an echo command. In this script, we have assigned the DEPARTMENT variable as “Computer Science”. Thus, the body of the first pattern case gets executed.

You can follow the instructions given below:

1. Open the terminal (Ctrl + Alt + T) and create the script:

department.sh 

2. Add these statements to the script:

#!/bin/bash DEPARTMENT="Computer Science" echo -n "Your DEPARTMENT is " case $DEPARTMENT in "Computer Science") echo -n "Computer Science" ;; "Electrical and Electronics Engineering" | "Electrical Engineering") echo -n "Electrical and Electronics Engineering or Electrical Engineering" ;; "Information Technology" | "Electronics and Communication") echo -n "Information Technology or Electronics and Communication" ;; *) echo -n "Invalid" ;; esac 

3. Each of the lines is described below:

4. Save the script and make it executable.

chmod +x department.sh

5. Run the script.

./department.sh 

Output:

406

Example 2: Using Multiple Patterns cases

In each clause, we can use multiple pattern cases. If the expression matches with at least one pattern case then the body gets executed of the clause. In this script, we have assigned the DEPARTMENT variable as “Electronics and Communication”. The third pattern case has two values separated by the pipe ( | ) operator. Thus, the body of the third pattern case gets executed.

You can follow the instructions given below:

1. Open the terminal (Ctrl + Alt + T) and create the script:

department.sh 

2. Add these statements to the script:

#!/bin/bash DEPARTMENT="Electronics and Communication" echo -n "Your DEPARTMENT is " case $DEPARTMENT in "Computer Science") echo -n "Computer Science" ;; "Electrical and Electronics Engineering" | "Electrical Engineering") echo -n "Electrical and Electronics Engineering or Electrical Engineering" ;; "Information Technology" | "Electronics and Communication") echo -n "Information Technology or Electronics and Communication" ;; *) echo -n "Invalid" ;; esac 

3. Each of the lines has been discussed below:

4. Save the script and make it executable.

chmod +x department.sh

5. Run the script.

./department.sh 

Output:

407Example 3: for Loops

In this script, we have assigned the department of three students of a class in an array DEPARTMENT. In other words, we are going to process more than one expression that is described in the DEPARTMENT variable. To iterate over multiple expressions, we can use a for a loop.

You can follow the instructions given below:

1. Open the terminal (Ctrl + Alt + T) and create the script:

department.sh 

2. Add these statements to the script:

#!/bin/bash DEPARTMENT=("Electronics and Communication" "Computer Science" "Information Technology") for value in "$" do case $value in "Computer Science") echo -n "Computer Science " ;; "Electrical and Electronics Engineering" | "Electrical Engineering") echo -n "Electrical and Electronics Engineering or Electrical Engineering " ;; "Information Technology" | "Electronics and Communication") echo -n "Information Technology or Electronics and Communication " ;; *) echo -n "Invalid " ;; esac done

3. Each of the lines is described below:

4. Save the script and make it executable.

chmod +x department.sh

5. Run the script.

./department.sh 

Output:

408

Example 4: Prompt User with Yes or No

You can follow the instructions given below:

1. Open the terminal (Ctrl + Alt + T) and create the script:

department.sh 

2. Add these statements to the script:

#!/bin/bash echo -n "Are you a student? [yes or no]: " read response case $response in "Y" | "y" | "YES" | "Yes" | "yes") echo -n "Yes, I am a student." ;; "N" | "n" | "No" | "NO" | "no" | "nO") echo -n "No, I am not a student."; ;; *) echo -n "Invalid input" ;; esac 

3. Each of the lines is explained below: