Bash – What does : ${param:=value} mean

bashshellzsh

I read the following in A User's Guide to the Z-Shell:

A synonym for ‘true’ is ‘:’; it’s often used in this form to give
arguments which have side effects but which shouldn’t be used —
something like

: ${param:=value}

which is a common idiom in all Bourne shell derivatives. In the
parameter expansion, $param is given the value value if it was empty
before, and left alone otherwise. Since that was the only reason for
the parameter expansion, you use : to ignore the argument. Actually,
the shell blithely builds the command line — the colon, followed by
whatever the value of $param is, whether or not the assignment
happened — then executes the command; it just so happens that ‘:’
takes no notice of the arguments it was given.

but I don't understand it. I get that : means true, but there are two colons in the expression. As a minor question, why is this idiom used so much in all Bourne shell derivatives? What purpose does it serve?

Note:
I am interested in what this idiom does in both bash and in zsh.

Thanks

Best Answer

Let's break this down into pieces.

This code runs the command : with some arguments. The command : does nothing and ignores its arguments. Therefore the whole command line does nothing, except whatever side effects happen in the arguments.

The syntax ${parameter_name:=value} exists in all non-antique Bourne-style shells, including ash, bash, ksh and zsh. It sets the parameter to a default if necessary. It is equivalent to

if [ -z "$parameter_name" ]; then parameter_name=value; fi
… ${parameter_name}

In other words, if parameter_name is not set or is set to an empty value, then set it to the indicated value; and then run the command, using the new parameter value. There is a variant, ${parameter_name=value}, which leaves the parameter empty if it was empty, only using the indicated value if the parameter was unset.

You'll find this syntax documented under “parameter expansion” in the POSIX spec, and the dash, bash, ksh and zsh manuals.

There are variations on this syntax, in particular ${parameter_name:-value} which let you use a default value for this expansion only, without assigning to the parameter.

In summary, : ${parameter_name:=value} is a concise way of writing

if [ -z "$parameter_name" ]; then parameter_name=value; fi