Pattern Dispatch With case

Match glob patterns and run the matching branch in a script.

macOSbashshellscriptingLow risk
Use case "$arg" in *.tar.gz) ...;; *.md) ...;; esac to dispatch on patterns.

01Command

Command
$ case "$1" in *.tar.gz) echo archive;; *.md) echo docs;; *) echo other;; esac

02What it does

case supports glob patterns such as *.tar.gz directly, which makes extension and argument dispatch concise and readable, without regex libraries. It is a standard shell construct for CLI tools.

03Risk

RiskLow risk
RequirementsmacOS default zsh/bash (bash 3.2+; some features need bash 4+ via Homebrew)
CompatibilitymacOS with bash 3.2+ (GNU bash 4+ via Homebrew for advanced features)
!Warning: Patterns are globs, not regex; . does not need escaping but regex quantifiers will not work.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

Last verified: 2026-10-13 · Category: Developer