Reputation: 25207
I do not understand the difference between ${var:-value}
and ${var:=value}
.
It seems that both commands in shell work identically.
What is the difference?
Upvotes: 0
Views: 280
Reputation: 54563
Both do something if the variable is unset or null (an empty value).
If you happen to be assigning the expression to something, or using it in a here-document, both may appear to be doing the same thing, since the unset/null var
is replaced by $value
.
But the difference is that the latter one also assigns $value
to var
, so that it can be used in subsequent shell expressions.
That is often used in this idiom:
: ${var:=value}
to assign the default value where none exists.
Upvotes: 1
Reputation: 247012
If var
is unset or null:
${var:-value}
will expand to "value"${var:=value}
will expand to "value" and set var=valueDemo
$ var=
$ echo "${var:-y}"; echo ">$var<"
y
><
$ echo "${var:=y}"; echo ">$var<"
y
>y<
Doc: https://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion
Upvotes: 2