Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Never seen this used before in BASH:


The part I've never seen/used before is the ${PIDFILE- part.

share|improve this question
up vote 10 down vote accepted

It means use $PIDFILE if $PIDFILE is defined, or /var/run/ if $PIDFILE is undefined.

Starting with a new shell:

$ echo ${PIDFILE-/var/run/}

Now define PIDFILE:

$ PIDFILE=/var/run/
$ echo ${PIDFILE-/var/run/}

It is from the old days of Bourne Shell sh man page.

          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.

          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}
$ echo ${NOSUCHVAR:-default}

$ set | grep NULLVAR      # produces output because NULLVAR is defined
$ echo ${NULLVAR-default}

$ echo ${NULLVAR:-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.

share|improve this answer
It's not "old way"/"new way". If there's no colon it only checks if the parameter is unset. If there is a colon, it checks whether it's unset or null. – Dennis Williamson Dec 20 '10 at 22:49
Missed that part. Thanks Dennis. Will update the answer. – Mikel Dec 20 '10 at 22:52
Thanks; the example and solution is very understandable. – drewrockshard Dec 20 '10 at 23:26


shouldn't it be pidfile=${PIDFILE:-/var/run/} the way you are explaining?

share|improve this answer
Both are valid. ${var-default} is the old way from Bourne Shell days. ${var:-default} is specified by POSIX. Both work in bash. – Mikel Dec 20 '10 at 22:49
Thanks much Mikel. – hari Dec 20 '10 at 22:59
Dennis rightly pointed out that there is a difference is the case of var being set but null. Updated my answer to reflect that. – Mikel Dec 20 '10 at 23:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.