Americas

  • United States
sandra_henrystocker
Unix Dweeb

Case statements on Linux explained

How-To
Jan 02, 20244 mins
Linux

Case statements allow your scripts to react differently depending on what values are being examined.

linux code binary
Credit: Shutterstock

The case statement in the bash shell provides an interesting and easy alternative to the more complex if statements. They represent the simplest form of the kind of logic that evaluates multiple values (e.g., “If it equals this, then do this. Otherwise, if it equals …). To see how if statements and case statements compare, take a look at the bash script code below that tests a numeric value.

#!/bin/bash

echo -n "Enter a whole number: "
read num

if [ $num -gt 100 ]
then
    echo "That number is greater than 100"
elif [ $num -lt 100 ]
then
    echo "That number is less than 100"
else
    echo "That number is exactly 100"
fi

That’s a fairly modest example. The variety of if statements that contain elif (else if) can go on for many more lines as in this script.

#!/bin/bash

DayOfWeek=`date +%A`

if [[ $DayOfWeek == "Monday" ]]; then
    echo "prep weekly agenda"
elif [[ $DayOfWeek == "Tuesday"  ]]; then
    echo "distribute meeting notes from last week"
elif [[ $DayOfWeek == "Wednesday"  ]]; then
    echo "check on supplies"
elif [[ $DayOfWeek == "Thursday"  ]]; then
    echo "meet with boss"
elif [[ $DayOfWeek == "Friday"  ]]; then
    echo "submit weekly report"
elif [[ $DayOfWeek == "Saturday"  ]]; then
    echo "hang out with friends"
elif [[ $DayOfWeek == "Sunday"  ]]; then
    echo "take a break"
fi

You can, however, do the same thing with case statements and they would be considerably easier – both to type and to read. Here’s the case statement version of the first if-then script shown above. Note that the line starting with *) is the equivalent of an “else”. If the value being tested doesn’t match any of those spelled out in the command, this is the command that will be run – the “default” command.

#!/bin/bash

echo -n "Enter a whole number: "
read num

case $num in
    100)  echo "$num is exactly 100";;
    [0-9][0-9]) echo "$num is less than 100";;
    *)    echo "$num is less than 100";;
esac

Here’s another example. Note that this script includes two case statements and that both include numeric ranges to demonstrate how this is done.

#!/bin/bash

echo -n "Enter a whole number: "
read num

if [ $num -gt 100 ]; then
echo Number cannor exceed 100
exit
fi

case $num in
100) echo $score equals 100 ;;
9[0-9]) echo "A" ;;
8[0-9]) echo "B" ;;
7[0-9]) echo "C" ;;
6[0-9]) echo "D" ;;
*) echo "F" ;;
esac

case $num in
100)  echo "$num is exactly 100";;
[0-9][0-9]) echo "$num is less than 100";;
*)    echo "$num is less than 100";;
esac

The second script above could be replaced with this:

#!/bin/bash

DayOfWeek=`date +%A`

case $DayOfWeek in
  Monday) echo "prep weekly agenda";;
  Tuesday) echo "distribute meeting notes from last week";;
  Wednesday) echo "check on supplies";;
  Thursday) echo "meet with boss";;
  Friday) echo "submit weekly report";;
  Saturday) echo "hang out with friends";;
  Sunday) echo "take a break";;
esac

Note that the ;; (double semicolons) at the end of each line in case statements are terminators and are necessary, but for a good reason. You could include multiple commands for each tested value, separating them with linefeeds or semicolons. The final two semicolons clearly mark the end of the commands to be run. Here are a couple syntax examples that would work identically:

case value in
    pattern1) action1; cmd1 ;;
    pattern2) action2; cmd2 ;;
    *) default_action ;;
esac

You could do the same thing like this:

case value in
pattern1)
    action1
    ;;
pattern2)
    action2 \
    ;;
*) default_action
    ;;
Esac

Wrap-up

Case statements provide a clear way to run commands dependent of whatever argument is being evaluated. The syntax makes it easy to see the list of patterns that might be matched and the commands that will be run of none match.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author