Back to CFM home             Brown University





The case statement

case is a flow control construct that provides for multi-way branching based on patterns.

Program flow is controlled on the basis of the wordgiven. This word is compared with each pattern in order until a match is found, at which point the associated command(s) are executed.

   case word in
   pattern1) command(s)
   ;;
   pattern2) command(s)
   ;;
   patternN) command(s)
   ;;
   esac

When all the commands are executed control is passed to the first statement after the esac. Each list of commands must end with a double semi-colon (;;).

A command can be associated with more than one pattern. Patterns can be separated from each other by a | symbol. For example:

   case word in
   pattern1|pattern2) command
   ...					;;

Patterns are checked for a match in the order in which they appear. A command is always carried out after the first instance of a pattern.

The * character can be used to specify a default pattern as the * character is the shell wildcard character.


[Home] [Search] [Index]