2013-07-17 88 views
3

我期待在bash腳本以下行:

: ${ROOT_DIR:="."} 

我相信我的理解是,第二部分是設定ROOT_DIR變量當前工作目錄的擴展。但是,我不確定ROOT_DIR是一個特殊的環境變量還是一個普通的變量。

此外,領先的「:」冒號究竟做了什麼?

回答

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.