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

后端 未结 3 831
生来不讨喜
生来不讨喜 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:13

    You should not change your login shell until you have a much better understanding of fish syntax and behavior. For example, in fish the equivalent of $$ is %self or $fish_pid depending on which fish version you are using. You should always specify the version of the program you are having problems with.

    Assuming you're using fish 2.x that would be written as

    if not set -q gnupg_SSH_AUTH_SOCK_by
    or test $gnupg_SSH_AUTH_SOCK_by -ne %self
        set -gx SSH_AUTH_SOCK "/run/user/$UID/gnupg/S.gpg-agent.ssh"
    end
    

    Also, notice that there is no equal-sign between the var name and value in the set -x.

    0 讨论(0)
  • 2021-01-15 07:17

    Since ${var:-value} expands to value if $var is empty, you can always replace it by writing your code out the long way:

    begin
      if test -n "$gnupg_SSH_AUTH_SOCK_by"
        set result "$gnupg_SSH_AUTH_SOCK_by"
      else
        set result 0
      end
    
      if [ "$result" -ne %self ]
        set -x SSH_AUTH_SOCK "/run/user/$UID/gnupg/S.gpg-agent.ssh"
      end
      set -e result
    end
    

    Note that I don't use (a) endorse, (b) condone the use of, or (c) fail to hold unwarranted prejudices against users of, fish. Thus, my advice is very much suspect, and it's likely that there are considerably better ways to do this.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题