Usage of “:=” and “:-” in shell script while exporting variable

后端 未结 1 1620
闹比i
闹比i 2021-01-25 08:12

In shell scripts we use

export VAR1=${VAR1:-KK}

export DATE=${DATE:=$(cat ${DATEDIR}/today_date)}

I have seen this in many scripts.

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-25 08:23

    From the Parameter expansion section of 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.

    There are many other options during parameter expansion, see the man page for all of them.

    0 讨论(0)
提交回复
热议问题