Using Switch Case in Unix Shell Scripting: Case-esac Statement

Overview of Unix Switch Case Statements:

In this tutorial, we will see how a switch case block can be used when creating conditional flow depending on the various values of a single expression.

Unix Shell The case-esac Statement: 

Unix Video #16:

The Shell Switch Case Syntax and Examples:

case <word> in
 <first pattern>)
 <second pattern>)
 <default statements>

Here, the value of the word expression is matched against each of the choice patterns.  If a match is found then the corresponding statements are executed until the ‘;;’ statement is encountered.  If there is no match, the default statements under ‘*)’ are executed.

The following is an Example of a switch case program:

 echo “Enter a number”
 read num
 case $num in
 echo “you have entered a single digit number”
 echo “you have entered a two-digit number”
 echo “you have entered a three-digit number”
 echo “your entry does not match any of the conditions”

To know more about working with loops in Unix, check our upcoming tutorial.

PREV Tutorial | NEXT Tutorial