Case statements allow your scripts to react differently depending on what values are being examined. 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. Related content how-to Compressing files using the zip command on Linux The zip command lets you compress files to preserve them or back them up, and you can require a password to extract the contents of a zip file. By Sandra Henry-Stocker May 13, 2024 4 mins Linux opinion NSA, FBI warn of email spoofing threat Email spoofing is acknowledged by experts as a very credible threat. By Sandra Henry-Stocker May 13, 2024 3 mins Linux how-to The logic of && and || on Linux These AND and OR equivalents can be used in scripts to determine next actions. By Sandra Henry-Stocker May 02, 2024 4 mins Linux how-to Using the apropos command on Linux By Sandra Henry-Stocker Apr 24, 2024 3 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe