The case Statement & Pattern Matching
Branch cleanly on many values. Learn the case statement, glob patterns, multiple-pattern matches, and how it beats long if-elif chains.
The case Statement & Pattern Matching is a free DevOps Bootcamp lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why case?
When a variable can take many values, a long if/elif/elif chain gets ugly. The case statement matches a value against patterns and is far more readable.
Basic Syntax
A case block tests a value against patterns, each ending in ) and a body terminated by ;;. The whole block ends with esac.
case "$fruit" in
apple) echo "red";;
banana) echo "yellow";;
esacThe Default Case
The *) pattern matches anything, acting as a catch-all default like else.
case "$cmd" in
start) echo "starting";;
stop) echo "stopping";;
*) echo "unknown command";;
esacMatching Multiple Patterns
Use | to match several patterns in one branch.
case "$ans" in
y|Y|yes) echo "Confirmed";;
n|N|no) echo "Cancelled";;
esacGlob Patterns
Patterns use shell globbing, not regex: * matches any string, ? any single character, and [...] a character class.
case "$file" in
*.txt) echo "text file";;
*.jpg|*.png) echo "image";;
esacCharacter Ranges
Bracket patterns match ranges, handy for grouping by first letter or digit.
case "$ch" in
[a-z]) echo "lowercase";;
[0-9]) echo "digit";;
esacA Simple Menu
case shines in menu-driven scripts where the user picks an option.
read -p "Choice: " opt
case "$opt" in
1) backup;;
2) restore;;
*) echo "Invalid";;
esaccase vs if
Use if for complex boolean conditions (ranges, comparisons, multiple variables). Use case when branching on the value or pattern of a single variable.
Quoting the Value
Quote the tested value ("$var") so empty or space-containing values are handled safely.
case "$input" in
"") echo "empty";;
*) echo "got: $input";;
esacFall-Through with ;;&
Bash supports ;;& to continue testing later patterns after a match, allowing multiple branches to fire.
case "$f" in
*.tar) echo "tar";;&
*.gz) echo "gzip";;
esacArgument Dispatch
A common real-world use: dispatch on the first script argument to implement subcommands like a CLI tool.
case "$1" in
init) do_init;;
build) do_build;;
*) echo "usage: $0 {init|build}";;
esacQuick Check
Test your understanding.
Recap
You learned the case statement: match a value against glob patterns, end branches with ;;, combine patterns with |, use *) as default, and prefer case over long if-elif chains for clean value-based and argument dispatch.
Frequently asked questions
Is the “The case Statement & Pattern Matching” lesson free?
Yes — the full text of “The case Statement & Pattern Matching” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “The case Statement & Pattern Matching”?
Branch cleanly on many values. Learn the case statement, glob patterns, multiple-pattern matches, and how it beats long if-elif chains. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The case Statement & Pattern Matching” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Looping Constructs (for, while)
- Functions in Bash Scripts
- Script Arguments and Options ($@, $#, getopts)
- The case Statement & Pattern Matching