TMPDIR 中的“-”是做什么的?

What a '-' in TMPDIR do?

我最近看到一个示例,其中调用命令时传递给 env 的选项如下:

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

$TMPDIR 中的 - 有什么作用?这是针对 linux.

的未指定版本

It sets $TMPDIR to itself if it's set, otherwise it sets it to /tmp.

来自documentation

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

因此,如果它为空或未设置,则将 TMPDIR 设置为 /tmp。如果省略:(例如${TMPDIR-/tmp},它只测试变量是否未设置,如指定的:

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.