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 find files on Linux There are many options you can use to find files on Linux, including searching by file name (or partial name), age, owner, group, size, type and inode number. By Sandra Henry Stocker Jun 24, 2024 8 mins Linux opinion Linux in your car: Red Hat’s milestone collaboration with exida With contributions from Red Hat and critical collaborators, the safety and security of automotive vehicles has reached a new level of reliability. By Sandra Henry Stocker Jun 17, 2024 5 mins Linux how-to How to print from the Linux command line: double-sided, landscape and more There's a lot more to printing from the Linux command line than the lp command. Check out some of the many available options. By Sandra Henry Stocker Jun 11, 2024 6 mins Linux how-to Converting between uppercase and lowercase on the Linux command line Converting text between uppercase and lowercase can be very tedious, especially when you want to avoid inadvertent misspellings. Fortunately, Linux provides a handful of commands that can make the job very easy. By Sandra Henry Stocker Jun 07, 2024 5 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe