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 How to examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe