fish: if error: `[: Missing argument at index 2`

fish: if error: `[: Missing argument at index 2`

我是 Fish 的新用户,目前我尝试将我的 Bash 提示符 (PS1) 转换为 fish_prompt,我将其保存到 ~/.config/fish/functions/fish_prompt.fish

这是一个与 this 类似的问题,但是,该解决方案对我不起作用。

我尝试删除 [],使用 test,但是,我不知道如何比较 Fish

中的字符串
if [ $prompt_hostname == 'remote-host' ]
    set userHost remote
else
    set userHost local
end

您需要引用变量,并将==替换为=(因为前者是bashism)。

问题是,就像在 bash 中一样,空变量在到达命令之前被删除,因此 [(这只是 test 的另一个名称)得到它的论点是这样的:

[ == 'remote-host' ]

这不是一个有效的表达式。

所以,这需要

if [ "$prompt_hostname" = 'remote-host' ]

if test "$prompt_hostname" = remote-host

(请注意,引用文字字符串是不必要的,但在两者中都是无害的 - 它不会改变任何内容,因为该字符串没有任何可以扩展的部分 - 没有 $,没有 *,没有 (), ....)