function export_if_empty {
[[ -z "$1" ]] && declare -gx "${1}"="$2"
}
export_if_empty 'XDG_CONFIG_HOME' "$HOME/.config"
export_if_empty 'XDG_DATA_HOME' "$HOME/.local/share"
export_if_empty 'XDG_CACHE_HOME' "$HOME/.cache"
export_if_empty 'XDG_DATA_DIRS' '/usr/share:/usr/local/share'
export_if_empty 'XDG_CONFIG_DIRS' '/etc/xdg'
unset -f export_if_empty
This does not work. even if the XDG_CONFIG_HOME is empty, -z flag fails to correctly identify it
export_if_empty '' JustATest
, and you will see that the-z
test is true.