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

瘦欲@ 提交于 2019-12-01 12:07:28

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!