: colon command for bash
Solution 1:
:
is a shell builtin that is basically equivalent to the true
command. It is often used as a no-op eg after an if
statement. You can read more about it in this question from stack overflow.
The ${varname=value}
basically means set the value of $varname
to value
if $varname
is not already set, and then return the value of $varname. Though if you try to run that at the command line it will try to run the value returned. Putting the :
in front as a no-op prevents bash from trying to run the value.
Note there are two slightly different forms:
${varname:=value}
sets varname to value if varname is either unset or null.
${varname=value}
only sets the value of varname if varname is currently unset (i.e., it will not change varname from "" to value)
(Thank you to chepner for clarifying that in a comment).
Someone else referencing this method