在Bash脚本中,${ROOT_DIR:="."}是什么意思?

3
我正在查看bash脚本中的以下行:
 : ${ROOT_DIR:="."}

我相信我理解第二部分是一个扩展程序,将ROOT_DIR变量设置为当前工作目录。但是,我不确定ROOT_DIR是特殊环境变量还是普通环境变量。
另外,冒号“:”的作用是什么?
1个回答

5

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.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接