AnyCompiler

Case Statements

case value in ... esac matches value against a series of patterns, each ending in ;;. Patterns can use glob-style wildcards (* matches anything) and multiple alternatives separated by | in a single branch, and * alone is the conventional default/fallback case.

fruit="apple"
case "$fruit" in
  apple)
    echo "It's an apple"
    ;;
  banana|plantain)
    echo "It's a banana-like fruit"
    ;;
  *)
    echo "Unknown fruit"
    ;;
esac
Output
It's an apple

Next: error handling, and a closing look at what this course covered.

Practice exercise

Given day="Saturday", use a case statement to print "weekend" for Saturday or Sunday, and "weekday" for anything else.