Bash 5 reference · Shell Builtins and Syntax

case (Bash builtin)

Match a value against glob patterns and run the first matching branch; cleaner than long if/elif chains.

Bash 5

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

case WORD in PATTERN) ... ;; *) ... ;; esac

Parameters

ParameterDescription
a|b) Several patterns for one branch.
*) The default branch.
;& ;;& Fall through / continue testing (rarely needed).

Examples

Matching file types
for f in a.jpg b.PNG c.txt; do
  case "${f,,}" in
    *.jpg|*.png) echo "$f: image" ;;
    *) echo "$f: other" ;;
  esac
done

Output

a.jpg: image
b.PNG: image
c.txt: other

Run this on your computer — see “Get Started” for setup.

Accept y/yes in any case
answer=Yes
case $answer in [Yy]*) echo confirmed ;; *) echo cancelled ;; esac

Output

confirmed

Run this on your computer — see “Get Started” for setup.

Was this page helpful?