这个任务是做什么的?

What does this assignment do?

我正在查看一个朋友的代码,他有一行:

dist=${dist:?Must set dist environment variable}

这条线是做什么的?将如何评估?是否有此类作业的文档?

谢谢

使用 man bash 解释得很好 — 或者在在线 Bash 手册中找到 parameter expansion。只需在终端提示符下输入 man bash 并在 :? 上搜索,您会发现:

${parameter:?word}
Display Error if Null or Unset. If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.

例子

让我们 运行 在未设置 dist 时执行该命令:

$ dist=${dist:?Must set dist environment variable}
bash: dist: Must set dist environment variable

因此,正如文档所说,dist 未设置的事实会导致显示错误消息。

现在,让我们为 dist 和 运行 相同的命令赋值:

$ dist=1
$ dist=${dist:?Must set dist environment variable}
$ echo $dist
1

由于dist被赋值,所以不会显示错误信息,dist的值保持不变。