Bash shell scripting: a colon difference

Have you ever wonder what the difference between ${name-“Hai Vu”} and ${name:-“Hai Vu”}? In other word, what is the difference does the colon make? To save you all the suspense: the colon works when a variable is either empty or unset. The construct without colon will only work when the variable is unset. To illustrate my point, I have created an example shell script (var_substitution.sh). Please save it to a file, examine, and run it to see my demo.

As always, please use the comment facility to post your questions, suggestions, or comments. I love to hear from you.
Hai

#!/bin/bash

# Demonstrate the subtle difference between ${var-word} and ${var:-word}

clear

printf “\n=================================\n”
printf “\${varName+word} and \${varName:+word} comparison\n\n”

varName=”Aloha”
echo “varName is not empty: the two constructs behave the same”
echo ” \${varName+Hello World}=${varName+Hello World}”
echo ” \${varName:+Hello World}=${varName:+Hello World}”
echo “”

unset varName
echo “varName is unset: they still behave the same way”
echo ” \${varName+Hello World}=${varName+Hello World}”
echo ” \${varName:+Hello World}=${varName:+Hello World}”
echo “”

varName=””
echo “varName is empty: this is where the two differs”
echo ” \${varName+Hello World}=${varName+Hello World}”
echo ” \${varName:+Hello World}=${varName:+Hello World}”
echo “”

printf “\n=================================\n”
printf “\${varName-word} and \${varName:-word} comparison\n\n”

varName=”Aloha”
echo “varName is not empty: the two constructs behave the same”
echo ” \${varName-Hello World}=${varName-Hello World}”
echo ” \${varName:-Hello World}=${varName:-Hello World}”
echo “”

unset varName
echo “varName is unset: they still behave the same way”
echo ” \${varName-Hello World}=${varName-Hello World}”
echo ” \${varName:-Hello World}=${varName:-Hello World}”
echo “”

varName=””
echo “varName is empty: this is where the two differs”
echo ” \${varName-Hello World}=${varName-Hello World}”
echo ” \${varName:-Hello World}=${varName:-Hello World}”
echo “”

echo ”
=================================
Conclusion
– The colon (:) construct works when a variable is empty AND unset
– The non-colon construct only works when the variable is unset

3 thoughts on “Bash shell scripting: a colon difference

  1. Passer By

    Congratulations. This is completely impenetrable to anyone who doesn’t already know what you are talking about!

  2. agambrahma

    Thanks!! — I came a cross a ${varname:-“blah”} being used in a shell script recently and I couldn’t figure out what it meant — this is exactly what I was looking for !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s