什么:$ {ROOT_DIR:="。"}在bash脚本中执行什么操作?

时间:2013-07-17 02:09:27

标签: bash shell unix

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

 : ${ROOT_DIR:="."}

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

此外,领先的“:”冒号究竟做了什么?

1 个答案:

答案 0 :(得分: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.