What is case statements in bash in Linux?

In Bash scripting, a case statement is a control structure that allows you to execute different blocks of code based on the value of a variable. It functions similarly to a switch statement found in many programming languages, providing a convenient way to handle multiple possible conditions.

bash scripting, case statements, control structures, switch statement, Linux

Learn how to utilize case statements in Bash for efficient decision-making in scripts, enabling cleaner code and improved readability.

#!/bin/bash

read -p "Enter a number (1-3): " number

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

bash scripting case statements control structures switch statement Linux