“esac” at the End of a Bash Case Statement

Bash scripting is a powerful way to automate tasks and manage Linux systems. It provides a flexible and powerful way to automate system tasks and perform various operations. One of the features of bash is the case statement that allows the user to execute the code based on certain conditions. One essential part of the bash case statement is “esac,” which will be discussed in detail in this article using the following content.

  • What Does “esac” Do? 
  • Benefits of Using the Bash Case Statement with “esac” 

What Does “esac” Work at the End of a Bash Case Statement?

The esac is a keyword used in Bash case statements to signify the end of the statement. It is essentially the reverse of the word “case,” which indicates the beginning of the case statement. Without it the script would not know when the case statement ends, leading to errors and unexpected behavior.

Syntax:

A user can understand the basic syntax of a Bash case statement by following the below code below:

case variable in
    pattern1)
        command1;;
    pattern2)
        command2;;
    *)
        default_commands;;
esac

Syntax Explanation:

  • The case variable has been written in the first line to test different conditions for the keyword “variable”.
  • If pattern1 or pattern2 is true after testing, command1 or command2 will be executed simultaneously.
  • If both are wrong, then the default_commands will be executed
  • esac is written at the end to terminate the case statement.

Example 1: Select a Number

Here’s a simple example of a Bash script using a case statement with esac:

#!/bin/bash
echo "Enter a number between 1 and 3:"
read number

case $number in
    1)
        echo "You entered one.";;
    2)
        echo "You entered two.";;
    3)
        echo "You entered three.";;
    *)
        echo "Invalid input.";;
esac

Code Explanation:

  • In this example, the script prompts the user to enter a number between 1 and 3.
  • The case statement then checks the user’s input and outputs a corresponding message.
  • The “esac” keyword at the end is used to terminate the case statement.

 A user can check the output by executing the bash script on the terminal using the below command:

$ bash bash_script.sh

The output shows that a user enters the number 2, which can be seen on the terminal.

Example 2: Select a Fruit

Another example of a bash case statement along with the esac at the end is written below:

#!/bin/bash
echo "Menu:"
echo "1. Apples"
echo "2. Oranges"
echo "3. Mangoes"
echo "4. Exit"
read -p "Enter your selection: " choice

case $choice in
    1)
        echo "You selected Apples."
        ;;
    2)
        echo "You selected Oranges."
        ;;
    3)
        echo "You selected Mangoes."
        ;;
    4)
        echo "Goodbye!"
        exit 0
        ;;
    *)
        echo "Invalid selection."
        ;;
esac

Code Explanation:

  • In this example, a menu has been created where a user has a choice to select from three different apples, which are Apples, Oranges, and Mangoes.
  • Users need to press 1 to 3 to select any fruit from the menu.
  • If a user wants to exit, they can press 4.

Advantages of Using esac 

There are several benefits to using a case statement with “esac” in your Bash scripts:

  • Improved Readability: The case statements make it easier to understand complex conditional logic.
  • Simplified Code: Using a case statement can reduce the number of lines of code in your script.
  • Easier Maintenance: The case statements are easier to modify and debug than conditional statements. 

Conclusion

The word “esac” is usually used at the end of a bash case statement that tells the script to end the code. This will improve user readability, simplify the code, and make it easy to maintain. This article has discussed the esac in detail along with the syntax by providing practical examples.