The shell has other (neater) constructs for doing setting and checking parameters.
${PARAM:-value} |
This means: if the parameter is UNSET or a NULL value, then substitute the value that has been set previously.
Using MAGIC, we can type:
echo ${MAGIC:-'zingzangzoom'} |
which should echo:
abracadabra |
Why?
Since MAGIC is NOT NULL, and NOT UNSET, the variable is used, thus abracadabra.
What happens if we unset the variable to give it a null value?
unset MAGIC echo ${MAGIC:-'zingzangzoom'} |
Now echo will print:
zingzangzoom |
One of the places that system administrators use this is:
${EDITOR:-/bin/vi} somefile |
If you haven't set your environment variable called EDITOR or it's set to a NULL value, then use the default editor vi to edit the file somefile.
If you have set EDITOR with:
EDITOR=/bin/emacs |
then you'd use emacs to edit the file somefile.
Notice:
unset $MAGIC echo ${MAGIC:-'zingzangzoom'} echo $MAGIC |
MAGIC is not being set to 'zingzangzoom'. The :- construct is not actually setting the value of MAGIC, it's just testing the value of MAGIC.