case statement is similar to switch case in other programming langauges.

It is used to compaer the given input with a multiple patterns, and commands inside a matching pattern are executed.

Syntax

case expression in

pattern1)
  ## Commands
  ;;
pattern1)
  ## Commands
  ;;
*)
  ## Default case to execute if none of the pattern is matched
  ;;
  • expression is an variable or valid expression to evaluate
  • It contains patterns defiend inside case which is evaluated with comparing expressions, matching case fuound, it executes commands inside it.
  • default case(*)) to execute if none of the pattern is matched
  • Each block of a pattern ends with a ;;
  • case is a beginning word and esac is a word terminate the case statement

Here is an example

name="john1"

case $name in
    "john")
        echo "John."
        ;;
    "others" | "others2")
        echo "Other names."
        ;;

    *)
        echo "Default Name."
        ;;
esac