next up previous contents
Next: The while command Up: Control Previous: The if command   Contents

The case command

Use case for multi-way branching depending on the value of a single expression.

echo -n "Enter the name of an animal: "
read ANIMAL
echo -n "The $ANIMAL has "
case $ANIMAL in
  horse | dog | cat) echo -n "four";;
  man | kangaroo ) echo -n "two";;
  *) echo -n "an unknown number of";;
esac
echo " legs."

This case command tests the value of the variable ``ANIMAL''. Each clause starts with one or more patterns terminated by a ``)''. This is followed by one or more commands, that note the double pair of ``;'' that ends a clause. The pattern ``*'' is a catch-all. case is terminated by esac.



P.D. Gronbech (IT Staff) 2015-10-02