Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does : ${ROOT_DIR:="."} do in a bash script?

Tags:

bash

shell

unix

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?

like image 423
ZenBalance Avatar asked Oct 28 '25 05:10

ZenBalance


1 Answers

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.
like image 196
Kevin Avatar answered Oct 29 '25 19:10

Kevin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!