Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between :- and := in Bash parameter substitution?

What's the difference between :- and := in Bash parameter substitution?

They seem to both set the default?

like image 811
Casebash Avatar asked Jan 12 '18 02:01

Casebash


People also ask

What does := do in bash?

Basically it will assign the value of word to parameter if and only if parameter is unset or null.

What does ${ parameter :- value Substitution operator does?

${var/Pattern/Replacement}First match of Pattern, within var replaced with Replacement. If Replacement is omitted, then the first match of Pattern is replaced by nothing, that is, deleted.

What is parameter substitution in bash?

${ parameter :- word } If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted. $ v=123 $ echo ${v-unset} 123. ${ parameter := word }

What is difference between $@ and $* in Linux?

The meaning of $* and $@ is identical when not quoted or when used as a parameter assignment value or as a file name. However, when used as a command argument, $* is equivalent to ``$1d$2d...'', where d is the first character of the IFS variable, whereas $@ is equivalent to $1 $2 ....


2 Answers

From https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html :

${parameter:-word}
If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter 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. Positional parameters and special parameters may not be assigned to in this way.

In :- does not modify the parameter value, just 'prints' the expansion of word. In := the parameter gets the new value that is the expansion of word and also it 'print' the expansion of word.
Sometimes in scripts you want to assign a default value to a variable if it was not set. Many use VAR=${VAR:-1}, which will assign '1' to VAR if VAR was not set. This may be also written as : ${VAR:=1}, which will assign '1' to VAR if VAR was not set and run : $VAR or : 1, but : is a special builtin in bash and will discard all arguments and do nothing.

like image 102
KamilCuk Avatar answered Sep 30 '22 20:09

KamilCuk


Quoting Bash Reference Manual:

${parameter:-word}

If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter 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. Positional parameters and special parameters may not be assigned to in this way.

The difference is that := doesn't only substitute the word, it also assigns it to the parameter:

var=
echo "$var"               # prints nothing
echo "${var:-foo}"        # prints "foo"
echo "$var"               # $var is still empty, prints nothing
echo "${var:=foo}"        # prints "foo", assigns "foo" to $var
echo "$var"               # prints "foo"

See this great wiki.bash-hackers.org tutorial for more information.

like image 26
PesaThe Avatar answered Sep 30 '22 19:09

PesaThe