While I do love the command-line, and will throw shell script at most problems
if it’s the right tool for the job, it’s actually fairly rare that I write
Generally speaking, my shell scripts will usually interrogate a variable or two, never too crazy or anything.
With that, recently I was writing a shell script for work and given the number
of conditionals I needed, it made sense to venture down the path of a
If you’re familiar with
switch statements in other languages, the
statement is the same deal, but with slightly different syntax. Instead of
opening the statement with
switch we’ll be using
case and each of our
patterns to match will be inside of that (without the familiar
case prefix on
Similar to how
if statements are finished with a
statements do the same, finishing up with
#!/usr/bin/env bash variable="foo" case variable in # This is where our patterns to match will go... esac
As mentioned, the patterns to match will be listed in between
but before we list those out, let’s add a catchall (similar to
#!/usr/bin/env bash variable='foo' case variable in # This is where our other patterns to match will go... *) echo 'We did not match any other pattern' ;; esac
It’s always good to have a catchall, think of it like an
else statement, which
you can use to catch unexpected values.
With the catchall in place, let’s add a couple of patterns to match again:
#!/usr/bin/env bash variable='foo' case variable in foo) echo 'We matched foo!' ;; bar) echo 'We matched bar!' ;; *) echo 'We did not match any other pattern' ;; esac
The sky’s the limit, add as many (or as few) as you need. Hopefully it’s
obvious, but if you need to a single pattern, you’re better off using an
Now let’s say that we wanted to run the same block of code against different patterns that are matched. You certainly could duplicate the entire pattern block, but there’s a way better way.
The patterns listed are able to take a pipe
| to list out multiple patterns to
#!/usr/bin/env bash variable='foo' case variable in foo | spam) echo 'We matched foo or spam!' ;; bar | eggs) echo 'We matched bar or eggs!' ;; *) echo 'We did not match any other pattern' ;; esac
Same deal, list out as many or as few as you need!