Q:

How to understand ${var:-world}

# echo ${var}

# echo ${var:-world}
world
# var='hello'
# echo ${var}
hello
# echo ${var:-world}
hello

Could you explain why the second echo ${var:-world} output hello other than world?
By the way, what's the topic called in English? I searched cut string, sub string...
Thanks.

A:

${name:-alternate} is a parameter expansion with a default value.  From the 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.

The first ${var:-world} printed world because $var was unset.  The second printed hello because you had set $var=hello, so $var was no longer unset.  Therefore, the default (world) was ignored.
More info is at the bash-hackers wiki.

