$variable
and ${variable}
into their respective contents. Conditional parameter expansion uses an operator following the variable name (inside braces) to indicate conditional expansion.=
${var=default}
:=
${var:=default}
-
${var-default}
:-
${var:-default}
+
${var+alternate}
:+
${var:+alternate}
${var=default
and ${var:=default}
) with a shell built-in such as :
allows you to assign default values to one or more variables. Since the : built-in meets these requirements:true
and false
also meet the criteria, but false
has an undesirable affect on exit status and true
, while identical in functionality to :
, is not guaranteed to adhere to that criterion.foo
and bar
are both unset when the above code runs:foo=abc
and bar=123
when the code runs:foo
and bar
are both set but empty (NULL):foo
remains empty because the =
operator (assign default value if unset) was used while bar
gets set to the default value because the :=
operator (assign default value if NULL or unset) was used.:
built-in or similar.: ${foo=bar} ${bar:=baz}
may be translated into : bar baz
which upon execution will give the arguments bar
and baz
to the :
command. Hence why it is important to use a built-in such as : that ignores any arguments given.