2

I recently saw an example where a command was invoked with the following option passed to env:

TMPDIR="${TMPDIR:-/tmp}"

What does the - in $TMPDIR do? This was for an unspecified version of linux.

pythonic metaphor
  • 10,296
  • 18
  • 68
  • 110

2 Answers2

5

From the documentation:

${parameter:-word}
If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

So this set TMPDIR to /tmp if it's empty or not set. If you leave out : (e.g. ${TMPDIR-/tmp}, it only tests whether the variable is not set, as specified:

Omitting the colon results in a test only for a parameter that is unset. Put another way, if the colon is included, the operator tests for both parameter’s existence and that its value is not null; if the colon is omitted, the operator tests only for existence.

Barmar
  • 741,623
  • 53
  • 500
  • 612