bashscriptingcolon-equals

Bash script what is := for?


Does anyone know what is := for?

I tried googling but it seems google filters all symbol?

I know the below is something like checking if the variable HOME is a directory and then something is not equal to empty string.

  if [ "${HOME:=}" != "" ] && [ -d ${HOME} ]

Solution

  • From Bash Reference Manual:

    ${parameter:=word}

    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.

    Basically it will assign the value of word to parameter if and only if parameter is unset or null.