Thursday, April 17, 2008

${VAR:-}: Mystery Solved

I was mystified, yesterday, to find that nearly 5% of the lines in /etc/init.d/functions, in my Fedora 8 distro, had a construct that looked like this: ${VAR:-}.

I use ${VAR:-value} a lot; it provides a default value if $VAR is empty or unset. But without a value? "If it's unset or empty, make it empty."

Huh?

I posted to the Boulder Linux Users Group mailing list, and Sean Reifschneider, of tummy.com, jumped in promptly, with the answer.

If you run the script under bash -u (or with set -u), unset variables cause the script to fail. This construct ensures that the variables will be set, though to null
, so the script completes.

The sound you hear is my palm smacking my forehead.

(Thanks, Sean!)

No comments: