I am looking at the following line in a bash script:
: ${ROOT_DIR:="."}
I believe I understand that the second part is an expansion that sets the ROOT_DIR variable to the current working directory. However, I am not sure if ROOT_DIR is a special environment variable or just a general one.
Also, what exactly does the leading ":" colon do?
ROOT_DIR is not special, it's just a variable this shell script happens to use.
: does nothing. In particular, it is used here as a dummy command to allow the side effect of := to take effect, assigning the default value to ROOT_DIR. You can get a bit more detail from the bash man page:
: [arguments]
No effect; the command does nothing beyond expanding arguments and
performing any specified redirections. A zero exit code is returned.
"Expanding arguments" is the important part here, it allows the default assignment to occur.
${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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With