Switch (Case) Statment in Bash

Switch (Case) Statment in Bash

Switch (Case) Statement in Bash

The case statement in Bash is a conditional construct that simplifies checking multiple conditions. It’s often used when comparing a variable or expression against multiple patterns and executing corresponding commands.

Why Use a Case Statement?

  • Readability: Easier to understand compared to nested if-elif statements.
  • Efficiency: Suitable for matching multiple cases quickly.
  • Flexibility: Supports pattern matching with wildcards and ranges.

Syntax

case variable in pattern1) # Commands for pattern1 ;; pattern2) # Commands for pattern2 ;; *) # Default commands (optional) ;; esac

Key Points:

  • variable is the value to evaluate.
  • pattern is the condition to match. It can include wildcards (*, ?) or ranges ([a-z]).
  • ;; marks the end of a block of commands.
  • * serves as the default case (executed if no other pattern matches).
  • esac marks the end of the case block.

Basic Example: Menu Selection

echo "Choose an option:" echo "1) Start" echo "2) Stop" echo "3) Exit" read -r choice case $choice in 1) echo "Starting the process..." ;; 2) echo "Stopping the process..." ;; 3) echo "Exiting..." ;; *) echo "Invalid option. Please choose 1, 2, or 3." ;; esac

Output (if user inputs 1):

Starting the process...

Advanced Example: File Type Checker

read -p "Enter a filename: " filename case $filename in *.txt) echo "This is a text file." ;; *.jpg | *.png) echo "This is an image file." ;; *.sh) echo "This is a shell script." ;; *) echo "Unknown file type." ;; esac

Output (if user inputs image.jpg):

This is an image file.

Example: Using Ranges

read -p "Enter a number: " num case $num in [1-5]) echo "You entered a number between 1 and 5." ;; [6-9]) echo "You entered a number between 6 and 9." ;; *) echo "Number is out of range." ;; esac

Case Statement with Command Substitution

You can evaluate the result of a command using case:

os_type=$(uname) case $os_type in Linux) echo "You are using Linux." ;; Darwin) echo "You are using macOS." ;; *) echo "Unknown operating system." ;; esac

Handling Multiple Patterns

You can combine patterns using | (logical OR):

read -p "Enter a fruit: " fruit case $fruit in apple|banana|cherry) echo "This is a common fruit." ;; mango|pineapple) echo "This is a tropical fruit." ;; *) echo "Unknown fruit." ;; esac

Output (if user inputs apple):

This is a common fruit.

Using Functions with Case Statements

You can pair functions with case to keep your script modular:

start_service() { echo "Starting the service..." } stop_service() { echo "Stopping the service..." } case $1 in start) start_service ;; stop) stop_service ;; *) echo "Usage: $0 {start|stop}" ;; esac

Conclusion

The case statement in Bash is a powerful tool for handling multiple conditions elegantly. It’s versatile, supports pattern matching, and is much cleaner than using multiple if-elif statements. Whether you're writing a menu-driven script, checking file types, or handling user input, the case statement is your go-to solution.

Let me know if you'd like additional examples or adjustments!

Souy Soeng

Souy Soeng

Hi there šŸ‘‹, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
šŸ‘Æ I’m looking to collaborate on open-source PHP & JavaScript projects
šŸ’¬ Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

Post a Comment

CAN FEEDBACK
close