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. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, and your progress syncs across the web and the CoddyKit app. The DevOps Bootcamp course includes 4 lessons in total.
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.
Learn DevOps Bootcamp with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 142
- Lessons
- 568
Frequently Asked Questions
Is the “The case Statement & Pattern Matching” lesson free?
Yes — any 3 lessons of the DevOps Bootcamp learning path, including “The case Statement & Pattern Matching”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The DevOps Bootcamp course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
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