0Pricing
Linux Command Line & Bash Scripting Mastery · Lesson

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.

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";;
esac

All lessons in this course

  1. Looping Constructs (for, while)
  2. Functions in Bash Scripts
  3. Script Arguments and Options ($@, $#, getopts)
  4. The case Statement & Pattern Matching
← Back to Linux Command Line & Bash Scripting Mastery