Shell parameter expansion with :+ is useful

| 2 min read

Use the shell parameter expansion form :+ for expanding optional values

I've been increasing my Bash scripting activities recently, not least in relation to some live stream episodes relating to Enterprise Messaging and have used some of the shell parameter expansion facilities described in section 3.5.3 Shell Parameter Expansion of the GNU Bash manual. In particular, I've been using what I call the "default value" (:-) form:

${parameter:-word}

This form has this description: "If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted" and is very useful for setting default value for parameters that are expected at invocation time, for example.

On a walk yesterday I was listening to an episode of a series of podcasts I'd discovered that very day, on Hacker Public Radio. It's an in-depth series on Bash scripting, and has quite a few episodes, some very recent, and the early ones dating back to 2010. The episode I listened to was hpr1648 :: Bash parameter manipulation by Dave Morriss, and I enjoyed it very much.

One of the things Dave mentioned was this form of expansion (:+), related to the one above, but sort of the opposite:

${parameter:+word}

The form has this description: "If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted." Dave found this slightly odd, and commented that he couldn't quite think of a use case for this form. I couldn't, either.

Later that same day, I came across a live streamer on Twitch - Rob, aka rwxrob - who has some excellent content, also on YouTube. Watching the beginning of one of his live stream recordings, Google Shell Scripting Guide, Yes, Yes, 1000 Times Yes!, he introduces a Bash shell scripting resource from Google - the Shell Style Guide, which he goes through in detail in the live stream.

Noting how great that style guide looked, I start to read through it immediately. And what do I see, in the section on Quoting? An example of the :+ shell expansion form (in an illustration of something else entirely), which made complete sense and explained its real purpose! I couldn't believe it - discovering multiple complementary sources of information on Bash shell scripting on the same day? Goodness me.

Here's the example of that :+ shell expansion form taken directly from that section in the style guide:

git send-email --to "${reviewers}" ${ccs:+"--cc" "${ccs}"}

Look at that beautiful thing!

${ccs:+"--cc" "${ccs}"}

If there is a value in the ccs variable, use it, but in the expanded context of it being a value to the --cc switch used with the git command. The value (most likely one or more email addresses) would be of no use on its own, but put with --cc it makes complete sense. And the icing on the cake is that the :+ form substitutes nothing if the variable is null or unset, meaning there's no carbon-copying requested if there are no emails listed in the ccs variable.

Now that made my day. I was originally with Dave on not being able to think of a reason for the :+ form, and then whammo, there's a perfect example right there. Thanks Dave, thanks Rob, and thanks to the Googlers who wrote the style guide!