0

I am looking at parameter expansion using ${parameter:-word} versus ${parameter:=word}.

Here is some relevant documentation:

${parameter:-word} If parameter is unset or null, the expansion of word is substituted.

${parameter:=word} If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted.

Certainly, the key difference is in the first sentence of each description, substituted versus assigned.

But in actuality, I do not know what happens when something is substituted versus when something is assigned.

Finally, when is it appropriate to use one versus the other?

6
  • But if $parameter is not null or unset then this expression doesn't appear to do any assigning or substituting. Jul 7, 2021 at 23:42
  • Looks like the end result is the same. But echo "${parameter:-word}" does not actually assign word to parameter immediately,
    – Pietru
    Jul 7, 2021 at 23:43
  • I am using getopt to pass directory name and file type extensions. Am setting defaults with dir=${dir:-$PWD} and fltype=${fltype:="texi,org"}. But I do not know whether to use :- or :=.
    – Pietru
    Jul 7, 2021 at 23:47
  • It is still not clear to me the usefulness of using ${parameter:-word} rather than ${parameter:=word}.
    – Pietru
    Jul 8, 2021 at 1:13

1 Answer 1

3

Consider this example:

#!/bin/bash

echo "${parameter:-a}"
echo "${parameter:-b}"
echo "${parameter:=c}"
echo "${parameter:=d}"

And an example run of the script:

$ bash ex.sh
a
b
c
c

In the first echo, parameter is unset, so a is substituted. This prints a.

In the second echo, parameter is still unset, so b is substituted. This prints b.

In the third echo, parameter is still unset, so parameter is assigned the value c, then parameter is evaluated. This prints c.

In the fourth echo, parameter is set (it was set to c in the previous statement), so its value is printed. This prints c again.

From this, you can see that ${parameter:-word} evaluates to word if parameter is unset or null, but does not affect the value of parameter, while ${parameter:=word} will set the value of parameter if it was unset of null.

6
  • Thank you for the example. One thing I cannot figure out are the circumstances that would push you to use ${parameter:-a}. I currently use ${parameter:=d} to set default parameters for function arguments.
    – Pietru
    Jul 8, 2021 at 2:20
  • @Pietru you can use ${parameter:-word} if you want the script to be able to determine that parameter was unset/null after you initially try to use its value. Jul 8, 2021 at 2:23
  • Does your comment also mean that I can do varp=${parameter:-word} ? Meaning set varp to word without setting parameter. However, I do not see a difference using parameter=${parameter:-word} and parameter=${parameter:=word}.
    – Pietru
    Jul 8, 2021 at 2:30
  • @Pietru Does your comment also mean...? Yes. However, I do not see a difference... There really isn't a difference in that case. Jul 8, 2021 at 2:32
  • Some people denigrate the use of parameter=${parameter:=word}, as it is a form of parameter expansion that intrinsically performs variable assignment, only to redundantly assign the result of the expansion to the same variable once again. How is ${parameter:=word} customarily used ?
    – Pietru
    Jul 8, 2021 at 2:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.