Case Statement in Bash

3 minute read

The case statement in Bash is a powerful tool for handling multiple conditions in a concise and readable manner. It is particularly useful when you need to compare a variable against multiple values. In this tutorial, we’ll delve into the syntax and usage of the case statement in Bash, accompanied by practical examples.

Basic Syntax of the case Statement

The basic syntax of the case statement in Bash is as follows:

case expression in
  pattern1)
    # Commands to be executed if expression matches pattern1
    ;;
  pattern2)
    # Commands to be executed if expression matches pattern2
    ;;
  pattern3)
    # Commands to be executed if expression matches pattern3
    ;;
  *)
    # Commands to be executed if no patterns match
    ;;
esac

The expression is evaluated against each pattern, and the commands within the matching pattern block are executed. The * (asterisk) serves as a wildcard for unmatched patterns.

Examples of Using the case Statement

Basic Case Statement

#!/bin/bash

fruit="apple"

case $fruit in
  "apple")
    echo "It's a delicious apple!"
    ;;
  "orange")
    echo "It's a juicy orange!"
    ;;
  "banana")
    echo "It's a ripe banana!"
    ;;
  *)
    echo "Unknown fruit. Please try again."
    ;;
esac

In this example, the case statement compares the value of the variable fruit against different fruit patterns. Depending on the value, it outputs a corresponding message.

Numeric Range Check

#!/bin/bash

score=75

case $score in
  [90-100])
    echo "Excellent! You got an A."
    ;;
  [80-89])
    echo "Good job! You got a B."
    ;;
  [70-79])
    echo "Not bad! You got a C."
    ;;
  *)
    echo "You need to improve your score."
    ;;
esac

Here, the case statement checks the value of the variable score against numeric ranges and provides feedback based on the range.

Checking for Multiple Values

#!/bin/bash

day="Friday"

case $day in
  "Monday" | "Wednesday" | "Friday")
    echo "It's a workday."
    ;;
  "Saturday" | "Sunday")
    echo "It's the weekend!"
    ;;
  *)
    echo "Invalid day."
    ;;
esac

This example demonstrates how to check for multiple values using the | (pipe) symbol as a logical OR.

Using Variables in Patterns

You can use variables and expressions in patterns to create dynamic comparisons.

#!/bin/bash

animal="dog"
sound=""

case $animal in
  "dog")
    sound="Woof woof!"
    ;;
  "cat")
    sound="Meow meow!"
    ;;
  "duck")
    sound="Quack quack!"
    ;;
  *)
    sound="Unknown animal sound."
    ;;
esac

echo "The $animal says: $sound"

Here, the case statement determines the sound of the animal based on the value of the animal variable.

Conclusion

The case statement in Bash is a versatile tool for handling multiple conditions in a clear and efficient manner. Whether comparing strings, numeric ranges, or checking for multiple values, the case statement simplifies the logic of your scripts.

Now equipped with the knowledge from this tutorial, you can utilize the case statement to create more readable and maintainable Bash scripts that handle diverse scenarios.


Frequently Asked Questions

  1. Can I use variables in patterns within a case statement?
    Yes, you can use variables and expressions in patterns to create dynamic comparisons.

  2. Is it possible to check for multiple values using the case statement?
    Yes, you can use the | (pipe) symbol as a logical OR to check for multiple values in a single pattern.

  3. What happens if no patterns match in a case statement?
    If no patterns match, the commands within the *) (asterisk) block are executed, serving as a default case.

  4. Can I nest case statements in Bash?
    Yes, you can nest case statements to create more complex conditional structures.

  5. Is there a limit to the types of conditions that can be checked using the case statement?
    The case statement is versatile and can be used for various conditions, including string comparisons, numeric ranges, and multiple value checks.

Updated: