简体   繁体   中英

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

In shell scripts we use

export VAR1=${VAR1:-KK}

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

I have seen this in many scripts.

Please explain me or refer a good link for why :- and := is being used and where exactly should it be used.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM