๐ Bash Case Statements โ Use case, in, and esac for Multi-Condition Scripts
๐งฒ Introduction to Bash Case Statements โ Simplify Multi-Condition Checks
When a script needs to evaluate multiple possible values of a variable and perform actions accordingly, case statements offer a cleaner alternative to multiple if-elif-else blocks. The Bash case command works like a switch-case construct in other programming languages and is ideal for handling menu selections, argument parsing, and input validation.
๐ฏ In this article, youโll learn:
- How to write a
casestatement in Bash - Syntax and structure of
case,in, andesac - Pattern matching in
caseblocks - Practical use cases and script examples
๐งพ Basic Syntax of case Statement
case $variable in
pattern1)
command1
;;
pattern2)
command2
;;
*)
default_command
;;
esac
| Keyword | Purpose |
|---|---|
case | Begins the statement |
in | Begins the list of patterns |
) | Ends the pattern and starts code |
;; | Ends the current block |
* | Wildcard for unmatched cases |
esac | Ends the statement (case in reverse) |
๐งช Example 1: Simple Menu Using case
echo "Choose an option: start | stop | status"
read action
case $action in
start)
echo "Starting service..."
;;
stop)
echo "Stopping service..."
;;
status)
echo "Service status: Running"
;;
*)
echo "Invalid option"
;;
esac
โ Sample Run:
Choose an option: start | stop | status
> start
Starting service...
๐งช Example 2: Pattern Matching with Wildcards
read -p "Enter filename: " file
case $file in
*.txt)
echo "Text file"
;;
*.sh)
echo "Shell script"
;;
*)
echo "Unknown file type"
;;
esac
โ Output:
Enter filename: test.sh
Shell script
๐ก You can match file extensions, numbers, or wildcard patterns.
๐งช Example 3: Case with Command-Line Arguments
case "$1" in
start)
echo "App started"
;;
stop)
echo "App stopped"
;;
restart)
echo "App restarted"
;;
*)
echo "Usage: $0 {start|stop|restart}"
;;
esac
โ Run:
./script.sh restart
โ Output:
App restarted
๐ง Why Use case Instead of if-elif-else?
| Feature | case | if-elif-else |
|---|---|---|
| Syntax readability | โ Cleaner | โ Verbose with many conditions |
| Pattern matching | โ Built-in glob | โ Manual with [[ ]] |
| Command-line parsing | โ Ideal | โ Possible but less elegant |
๐งฑ Advanced Patterns
You can use multiple matches with |:
case $input in
y|Y|yes|YES)
echo "You said yes."
;;
n|N|no|NO)
echo "You said no."
;;
*)
echo "Unknown input."
;;
esac
๐ Summary โ Bash Case Statements
The case statement in Bash is your go-to tool for handling multiple input conditions clearly and efficiently. It’s a perfect fit for user menus, argument validation, file type checks, and automation logic.
๐ Key Takeaways:
- Use
case,in,esacto check multiple input patterns ;;separates each case block*acts as a default (catch-all)- More readable and scalable than nested
ifstatements
โ๏ธ Real-world Uses:
- Parsing script parameters
- Creating interactive terminal menus
- Handling file type conditions in automation scripts
โ FAQ โ Bash Case Statements
โ Can I use regex in a Bash case statement?
โ
Not full regex, but you can use glob patterns like *.txt, [0-9]*, etc.
โ What does ;; mean in a case statement?
โ
It ends the current condition block and prevents fall-through to the next one.
โ Is case faster than if in Bash?
โ
Slightly more efficient in matching many conditions and much more readable for multiple options.
โ Can I use variables inside patterns?
โ Not directly. Pattern matching must use literal values or glob-style patterns, not variable substitution.
โ How do I add a default action in a case statement?
โ
Use *:
*)
echo "Default case"
;;
Share Now :
