Never seen this used before in BASH:
pidfile=${PIDFILE-/var/run/service.pid}
The part I've never seen/used before is the ${PIDFILE-
part.
It means use $PIDFILE
if $PIDFILE
is defined, or /var/run/service.pid
if $PIDFILE
is undefined.
Starting with a new shell:
$ echo ${PIDFILE-/var/run/service.pid}
/var/run/service.pid
Now define PIDFILE:
$ PIDFILE=/var/run/myprogram.pid
$ echo ${PIDFILE-/var/run/service.pid}
/var/run/myprogram.pid
It is from the old days of Bourne Shell sh man page.
${parameter-word}
If parameter is set then substitute its value;
otherwise substitute word.
The other form you might have already seen is ${parameter:-word}
. It is similar, but behaves differently if parameter
is set to the empty string.
${parameter:-word}
Use Default Values. If parameter is unset or null,
the expansion of word is substituted. Otherwise,
the value of parameter is substituted.
To demonstrate:
$ set | grep NOSUCHVAR # produces no output because NOSUCHVAR is not defined
$ echo ${NOSUCHVAR-default}
default
$ echo ${NOSUCHVAR:-default}
default
$ NULLVAR=
$ set | grep NULLVAR # produces output because NULLVAR is defined
NULLVAR=
$ echo ${NULLVAR-default}
$ echo ${NULLVAR:-default}
default
Note how ${NULLVAR-default}
expands to the empty string, because NULLVAR
is defined.
For a full explanation, run "man bash" and search for Parameter Expansion by typing "/Parameter Expansion".
The ${parameter-word} bit is hidden away in this explanation:
When not performing substring expansion, using the forms documented below,
bash tests for a parameter that is unset or null. Omitting the colon results
in a test only for a parameter that is unset.
Thanks to Dennis for the correction about set versus null.
Mikel:
shouldn't it be
pidfile=${PIDFILE:-/var/run/service.pid}
the way you are explaining?