Usage of :- (colon dash) in bash

后端 未结 2 1637
暗喜
暗喜 2020-11-22 09:32

What is the meaning of this style in bash?

${PUBLIC_INTERFACE:-eth0}

What is the purpose of :-?

2条回答
  •  死守一世寂寞
    2020-11-22 09:50

    If $PUBLIC_INTERFACE exists and isn't null, return its value, otherwise return "eth0".

    There are actually a few of these documented in the bash man page:

    ${parameter:-word} Use Default Values. If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

    ${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.

    ${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.

    ${parameter:+word} Use Alternate Value. If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.

提交回复
热议问题