case Statement

noun

say it: “KAYSS STAYT-ment” /ˈkeɪs ˈsteɪtmənt/

A part of a script that compares one value with several patterns and runs the commands for the first one that matches.

What is a case statement in bash?

A case statement compares one value with a list of patterns and runs only the lines for the first one that matches. A pipe between patterns means or, *) matches anything else, ;; ends each choice, and esac, case backwards, ends the whole thing.

case "$answer" in
  y|Y|yes) echo "Backing up." ;;
  *)       echo "Nothing was backed up." ;;
esac

In a sentence

  • “A case statement on "$1" handles the --help and --list options.”
  • “The case statement treats y, Y and yes as the same answer.”

Level: Apprentice · More Unix terms at this level

Updated