I read the bash man page on this, but I do not understand the difference. I tested both of them out and they seem to produce the exact same results.
I want to set a default value of a variable if the value was not set via a command-line parameter.
#!/bin/bash
var="$1"
var=${var:-word}
echo "$var"
The code above echoes word
if $1
is null and echoes value of $1
if not null. So does this:
#!/bin/bash
var="$1"
var=${var:=word}
echo "$var"
According to Bash man page,
${parameter:-word}
Use Default Values. If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
${parameter:=word}
Assign Default Values. 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.
Is it that they are the same and the ${parameter:=word}
just does more?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…