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

3

17

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

They seem to both set the default?

Conifer answered 12/1, 2018 at 2:35 Comment(1)
The practical difference is Positional parameters and special parameters may not be assigned using :=. (they can with :-)Walleye
C
17

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.

Cadent answered 12/1, 2018 at 2:38 Comment(2)
How is "${var:=foo}" different from "${var=foo}" then?Halfcaste
@Halfcaste From Bash Reference Manual: If the colon is included, the operator tests for both parameter’s existence and that its value is not null; if the colon is omitted, the operator tests only for existence.Cadent
M
2
$ var=
$ echo $(( ${var:-1} + 3 ))  # local substitution if value is null
4
$ echo $(( ${var} + 3 ))
3

# set it to null 
$ var= 
$ echo $(( ${var:=1} + 3 )) # global substitution if value is null
4
$ echo $(( ${var} + 3 ))
4 

https://www.tldp.org/LDP/abs/html/parameter-substitution.html

Milkman answered 12/1, 2018 at 2:40 Comment(0)
G
2

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.

Georgy answered 12/1, 2018 at 2:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.