brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ${var:-default} gives a shell variable a fallback, ${var:=x} also sets it

Shell
~/tips/tip-shell-param-default-values

backup_dir=${BACKUP_DIR:-/var/backups}

Uses a default value when a variable is unset or empty.

I use defaults so a script works without any setup and can still be tuned from the environment.

backup_dir=${BACKUP_DIR:-/var/backups}
: "${RETRIES:=3}"
echo "$backup_dir $RETRIES"

${var:-word} expands to word when the variable is unset or empty and leaves the variable alone. ${var:=word} also assigns it. The leading : is a no-op command that lets you run the assignment without executing the value. Leave out the colon, as in ${var-word}, and an empty but set variable counts as set.

Further reading: Bash manual: Shell Parameter Expansion

More Shell tips zmv -n '(*).log' '$1.txt' zoxide query -ls project f=$(mktemp --suffix=.json) shopt globskipdots echo $EPOCHSECONDS All Shell tips (78) All topics

Link