What is the meaning of this style in bash?
${PUBLIC_INTERFACE:-eth0}
What is the purpose of :-?
:- is used in the ${parameter:-word} shell parameter expansion: if parameter is null or unset, it expands to the value of word, otherwise to the value of parameter.
Example:
$ str=
$ echo "${str:-default}"
default
This and the similar expansions using :=, :+ and :? all come in two flavours: with and without a colon. The difference is that the expansion with the colon kicks in for "null or unset", whereas without the colon, it's just "null".
Observe:
$ str= # Null, but not unset
$ echo "${str-default}" # Expands to value of $str, the empty string
$ echo "${str:-default}" # Expands to "default"
default
Where is this useful? A few examples:
Default values
${FCEDIT:-${EDITOR:-vi}}: $FCEDIT if defined, or else $EDITOR if defined, or else vi.A loop in a script that should read from a file if one is provided as an argument and from standard input otherwise could look like this:
while IFS= read -r line; do
# do something
done < "${1:-/dev/stdin}"
When using set -u
set -u is a handy way to force cleaner scripting by having the script die when encountering an unset variable, as promoted by for example this article (not that I endorse everything in there1). If we want to check if a certain variable has a value with [[ $var ]], the script now dies if var is unset, even though this might be legitimate.
The way around this is using [[ ${var:-} ]] instead, and set -u won't complain. (This is basically using a default value again, but the substituted value is the empty string in this case.)
These expansions are not exclusive to Bash, by the way: the POSIX shell spec has them all, too.
1 See also BashFAQ/112, What are the advantages and disadvantages of using set -u (or set -o nounset)?