The case Statement

The case statement is the second form of flow control available in the shell. Its syntax is as follows:

case word in
     pattern1)
            list1
            ;;
     pattern2)
            list2
            ;;
...
     patternN)
            listN
            ;;
esac

Here the string word is compared to each of the patterns from pattern1 to pattern. When a matching pattern is found, the list following the matching pattern is then executed.

When a list finishes executing, the special command ;; indicates that flow should jump to the end of the case statement. The ;; is similar to the break command in the C programming language. If no matches are found, the case statement does not perform any actions. The minimum number of patterns is one. There is no limit on the maximum number of patterns.

Some programmers ...

Get Sams Teach Yourself Shell Programming in 24 Hours, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.