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
Syntax
case WORD in PATTERN) ... ;; *) ... ;; esac
Parameters
| Parameter | Description |
|---|---|
a|b) |
Several patterns for one branch. |
*) |
The default branch. |
;& ;;& |
Fall through / continue testing (rarely needed). |
Examples
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.
answer=Yes
case $answer in [Yy]*) echo confirmed ;; *) echo cancelled ;; esac
Output
confirmed
Run this on your computer — see “Get Started” for setup.
Related
if· Run commands depending on whether a command succeeds (exit status 0), with optional elif and else branches.- Bash Conditionals: if, [[ ]] and case · tutorial