🐚 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 :
