[lug] What the heck can I use ${VAR:-} for?

Jeffrey Haemer jeffrey.haemer at gmail.com
Wed Apr 16 10:00:45 MDT 2008


My F8 /etc/init.d/functions is chock-a-block full of constructs like this:

   if [ "${BOOTUP:-}" != "verbose" ]; then

What's this mean?  Specifically, why's it different from this?

   if [ "$BOOTUP" != "verbose" ]; then

or -- unless, somehow, $BOOTUP contains blanks -- even this?

   if [ $BOOTUP != verbose ]; then

What are all those extra ":-" thingies about?

Background, in case you're reading this casually:

${VAR:-value} is a useful, compact way to return default values.  When $VAR
already has an interesting value, the expression returns it; if, however,
$VAR is null or unset, the expression returns "value".

Thus, ${VAR:-} returns nothing if $VAR is null or unset -- but so does $VAR,
without the extra hoo-hah, hence my puzzlement.  No experiment I've done so
far clears up this mystery, so I thought I'd ask.

Almost 5% of the lines in the file use ${VAR:-}, with no default value
following the ":-".  I figure, I'm just a forehead-smack away from learning
a cool, new trick.  :-)

-- 
Jeffrey Haemer <jeffrey.haemer at gmail.com>
720-837-8908 [cell]
http://goyishekop.blogspot.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lug.boulder.co.us/pipermail/lug/attachments/20080416/adc42a22/attachment.html>


More information about the LUG mailing list