What is the result of $ {VAR: = value} in a bash script

I am looking at several bash scripts that use :=between ${ }. For example, the template is as follows:

export VAR=${VAR:=value}

What does it do? Does it assign a value to VAR if the VAR does not exist?

+4
source share
3 answers

According to the documentation :

$ {parameter: = word}

If the parameter is not specified or null, the word extension is assigned to the parameter. Then the parameter value is replaced. Therefore, you cannot assign positional parameters and special parameters.

, VAR , /null, value.

+3

man:

   ${parameter:=word}
          Assign Default Values.  If  parameter  is  unset  or  null,  the
          expansion of word is assigned to parameter.  The value of param‐
          eter is then substituted.   Positional  parameters  and  special
          parameters may not be assigned to in this way.

, . unset/null, ​​ , .

+3

.

${foo:=bar}

$foo, . "bar" foo, . , . , :

: ${foo:=bar}

, , . ,

foo=${foo:-bar}

and I think this is more obvious to most readers.

+2
source

Source: https://habr.com/ru/post/1534881/


All Articles