Reputation: 10317
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?
Upvotes: 4
Views: 3027
Reputation: 56129
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.
Upvotes: 5