Bash script what is: = for?

Does anyone know what is: = for?

I tried googling, but does google seem to filter all characters?

I know that the below shows that the HOME variable is a directory, and then something is not equal to an empty string.

if [ "${HOME:=}" != "" ] && [ -d ${HOME} ] 
+6
scripting bash
source share
3 answers

From the Bash Reference Guide :

${parameter:=word}

If the parameter is not set or is equal to zero, the word extension is assigned the parameter. The parameter value is then replaced. positional parameters and special parameters may not be assigned in this way.

Basically, it assigns the value of word parameter if and only if parameter not set or null.

+14
source share

On the Bash man page:

Assign default values. If the parameter is not specified or is null, the word extension is assigned the parameter. The parameter value is then replaced. positional parameters and special parameters may not be assigned in this way.

Human pages are a wonderful thing. man bash will tell you almost everything you want to know about Bash.

+1
source share

Here "${HOME:=}" is more or less no-op, since it returns $ HOME if it is installed, and nothing if it is not.

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

This will work identically:

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

If you had something after the equal sign, the first part of if always be true.

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

no matter what happens, you always get a string not equal to "".

However, this will depend on whether the values โ€‹โ€‹of $ HOME and $ DEFAULT were zero:

 if [ "${HOME:=$DEFAULT}" != "" ] && [ -d ${HOME} ] 
0
source share

All Articles