我正在查看 bash 脚本中的以下行:

 : ${ROOT_DIR:="."}

我相信我明白第二部分是将 ROOT_DIR 变量设置为当前工作目录的扩展。但是,我不确定 ROOT_DIR 是特殊环境变量还是一般环境变量。

另外,前导的“:”冒号究竟是做什么的?

最佳答案

ROOT_DIR 并不特殊,它只是这个 shell 脚本碰巧使用的一个变量。: 什么都不做。特别是,这里用作一个伪命令,让 := 的副作用生效,将默认值赋给 ROOT_DIR 。您可以从 bash 手册页获得更多详细信息:

   : [arguments]
          No effect; the command does nothing beyond expanding arguments and
          performing any specified redirections.  A zero exit code is returned.

“扩展参数”是这里的重要部分,它允许默认赋值发生。
   ${parameter:=word}
          Assign Default Values.  If parameter is unset or null, the expansion of
          word is assigned to parameter.  The value of parameter is then  substituted.
          Positional parameters and special parameters may not be assigned to in this way.

关于bash - : ${ROOT_DIR: ="."} do in a bash script? 是什么,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17689930/

10-13 09:18