Case statements can allow you to simplify the logic of your scripts. Credit: Daboost/Shutterstock If you’ve never used bash case statements, you might be very surprised by how easily they allow you to simplify the logic of your scripts. Instead of composing a long group of code lines that would allow you to test a variable against a number of potential values, you could write a terse and delightfully readable chunk of code that’s easy to put together. When you use the if/then option, you have quite a bit of flexibility, but you can end up having to put a lot of code together. For example, you could test a variable and take an action based on its value in a script like this: #!/bin/bash echo -n "enter the number of equal sides that the shape has> " read numsides # make sure answer is numeric re='^[0-9]+$' if ! [[ $numsides =~ $re ]] ; then echo "error: Not a number" >&2; exit 1 fi if [ $numsides -lt 3 ]; then echo "Sorry, but that would NOT be a geometric shape" fi if [ $numsides == 3 ]; then echo triangle fi if [ $numsides == 4 ]; then echo square or rectangle fi if [ $numsides == 5 ]; then echo pentagon fi if [ $numsides == 6 ]; then echo hexagon fi if [ $numsides == 7 ]; then echo heptagon fi if [ $numsides == 8 ]; then echo octagon fi if [ $numsides == 9 ]; then echo nonaagon fi if [ $numsides == 10 ]; then echo decagon fi if [ $numsides == 11 ]; then echo hendecagon fi if [ $numsides == 12 ]; then echo dodecagon fi if [ $numsides -gt 12 ]; then echo "Hmm, you’d better ask Google" fi Notice that the script will exit with an error if the value entered isn’t numeric or is not provided. You can also take advantage of “elif” (else if) clauses to lessen the number of lines you need. Here’s a redo of the script above that displays this option. #!/bin/bash echo -n "enter the number of equal sides that the shape has> " read numsides # make sure answer is numeric re='^[0-9]+$' if ! [[ $numsides =~ $re ]] ; then echo "error: Not a number" >&2; exit 1 fi if [ $numsides -lt 3 ]; then echo "Sorry, but that would NOT be a geometric shape" elif [ $numsides == 3 ]; then echo triangle elif [ $numsides == 4 ]; then echo square or rectangle elif [ $numsides == 5 ]; then echo pentagon elif [ $numsides == 6 ]; then echo hexagon elif [ $numsides == 7 ]; then echo heptagon elif [ $numsides == 8 ]; then echo octagon elif [ $numsides == 9 ]; then echo nonagon elif [ $numsides == 10 ]; then echo decagon elif [ $numsides == 11 ]; then echo hendecagon elif [ $numsides == 12 ]; then echo dodecagon elif [ $numsides -gt 12 ]; then echo "Hmm, you’d better ask Google" fi Using case statements, on the other hand, makes your code much simpler to put together and much easier to read. #!/bin/bash echo -n "enter the number of equal sides that the shape has> " read numsides # make sure answer is numeric re='^[0-9]+$' if ! [[ $numsides =~ $re ]] ; then echo "error: Not a number" >&2; exit 1 fi case "$numsides" in 0-2) echo "Sorry, but that would NOT be a geometric shape";; 3) echo triangle;; 4) echo square or rectangle;; 5) echo pentagon;; 6) echo hexagon;; 7) echo heptagon;; 8) echo octogon;; 9) echo nonagon;; 10) echo decagon;; 11) echo hendecagon;; 12) echo dodecaggon;; *) echo "Hmm, you’d better ask Google" esac Each segment in the case statement must end with two semicolons. However, you could use multiple commands separated by semicolons and only use the double semicolons at the end od that segment. Wrap-up Notice how much easier case commands are to set up and maintain. You can provide any number of possible values at the cost of one line each. 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