1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Provide A Fallback Value For Unset Parameter as a unix til

This commit is contained in:
jbranchaud
2021-01-08 15:29:22 -06:00
parent b6cc6ba298
commit ba1cc59cba
2 changed files with 31 additions and 1 deletions

View File

@@ -0,0 +1,29 @@
# 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.
```bash
$ echo "${XDG_CONFIG_HOME}"
```
To make a script more robust, you can provide a fallback value (i.e. [default
value](https://wiki.bash-hackers.org/syntax/pe#use_a_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.
```bash
$ 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](https://unix.stackexchange.com/a/122848/5916)