[Next] [Previous] [Up] [Top] [Contents]

9.9.2 Conditional switch and case Sh

case parameter in

pattern1[|pattern1a]) command list1;;

pattern2) command list2

command list2a;;

pattern3) command list3;;

*) ;;


You can use any valid filename meta-characters within the patterns to be matched. The ;; ends each choice and can be on the same line, or following a <newline>, as the last command for the choice. Additional alternative patterns to be selected for a particular case are separated by the vertical bar, |, as in the first pattern line in the example above. The wildcard symbols,: ? to indicate any one character and * to match any number of characters, can be used either alone or adjacent to fixed strings.

This simple example illustrates how to use the conditional case statement.


case $1 in

aa|ab) echo A


b?) echo "B \c"

echo $1;;

c*) echo C;;

*) echo D;;


So when running the script with the arguments on the left, it will respond as on the right:

aa A

ab A

ac D

bb B bb

bbb D

c C

cc C

fff D

Introduction to Unix - 14 AUG 1996
[Next] [Previous] [Up] [Top] [Contents]