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

Jarrar Jaffari jarrar.jaffari at gmail.com
Wed Apr 16 10:50:29 MDT 2008


Jeffery,

The only thing I can think of is the developer is re-enforcing the fact
that, if BOOTUP is null then lets keep it NULL and that is why may be he
used :- and not :=. Note that := returns and assigns the value that follows
where as :- does half that is it just returns but does not assign the value
to the variable. But then I would have put a comment saying "in case of
BOOTUP is unset we wanna keep it that way".

I do not think we get any other useful  stuff from ${BOOTUP:-}, may be a
typo ?

Jarrar

On Wed, Apr 16, 2008 at 10:00 AM, Jeffrey Haemer <jeffrey.haemer at gmail.com>
wrote:

> 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
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> Join us on IRC: lug.boulder.co.us port=6667 channel=#colug
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lug.boulder.co.us/pipermail/lug/attachments/20080416/017ef372/attachment.html>


More information about the LUG mailing list