Provide A Fallback Value For Unset Parameter

If you are using a value in a parameter expansion expression that isn't set, the result will be empty.

For instance, the XDG paths are not defined for me on OSX.

$ echo "${XDG_CONFIG_HOME}"

To make a script more robust, you can provide a fallback value (i.e. default value). The parameter expansion will use the fallback value if the primary value is either unset or null.

The syntax for this is to follow the primary parameter with :- and then the fallback parameter.

$ echo "${XDG_CONFIG_HOME:-$HOME/.local/share}"
/Users/jbranchaud/.local/share

Because I'm on OSX, this expands to my $HOME directory with /.local/share appended.

source

Last updated