case

Expand words, and match against patern.

Syntax
      case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac

A case command first expands word, and tries to match it against each pattern in turn, using the same matching rules as for pathname expansion (see Pathname Expansion).

The word is expanded using tilde expansion, parameter and variable expansion, arithmetic substitution, command substitution, process substitution and quote removal.

Each pattern examined is expanded using tilde expansion, parameter and variable expansion, arithmetic substitution, command substitution, and process substitution. If the shell option nocasematch is enabled, the match is performed without regard to the case of alphabetic characters.

When a match is found, the corresponding list is executed. After the first match, no subsequent matches are attempted.

Here is an example using case in a script that could be used to describe one interesting feature of an animal:

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

The exit status is zero if no pattern matches. Otherwise, it is the exit status of the last command executed in list.

case is a bash builtin command

“In expanding the field of knowledge, we but increase the horizon of ignorance” ~ Henry Miller

Related macOS commands:

break - Exit from a loop
for - Loop command
while - Loop command
continue - Resume the next iteration of a while or for loop


 
Copyright © SS64.com 1999-2019
Some rights reserved