What's the equivalent to ${var:-defaultvalue} in fish?

后端 未结 3 935
生来不讨喜
生来不讨喜 2021-01-15 06:26

Hello I am trying to translate my .bashrc to fish format almost done, mostly is clear on the documentation but this part is giving me a headache.. is so my gnupg works with

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-15 07:27

    I had a similar question, related to XDG_* variables.

    var1="${XDG_CACHE_HOME:-$HOME/.cache}"/foo
    var2="${XDG_CONFIG_HOME:-$HOME/.config}"/foo
    var3="${XDG_DATA_HOME:-$HOME/.local/share}"/foo
    some-command "$var1" "$var2" ...
    

    What I found as the best alternative is to simply set univeral variables once for the defaults--

    set -U XDG_CACHE_HOME ~/.cache
    set -U XDG_CONFIG_HOME ~/.config
    set -U XDG_DATA_HOME ~/.local/share
    

    Then in fish config file(s) or scripts, simply use "$XDG_CONFIG_HOME"/.... The value of an exported environment variable will override the universal variable if set, otherwise the universal variable is there as a default/fallback. If the universal variable is used, it is not exported to child processes, while an exported environment variable is, which provides the full equivalent to bash|zsh parameter expansion.

提交回复
热议问题