简体   繁体   English

fish_prompt 中的 if 语句

[英]if statement in fish_prompt

I'm trying to customize my fish prompt but i can't seem to put an if statement in it.我正在尝试自定义我的鱼提示,但我似乎无法在其中添加 if 语句。
What I want: user@host ~/.config [127]>我想要什么: user@host ~/.config [127]>
What i tried:我尝试了什么:

function fish_prompt 
    echo -n $USER
    echo -n "@"
    echo -n $hostname
    echo -n " "
    echo -n (prompt_pwd)
    echo -n (__fish_git_prompt)
    if [ $status -ne 0 ] 
        echo -n " [$status] "
    end 
    echo -n "> "
end 

I want the $status to show only when there is an error.我希望 $status 仅在出现错误时显示。 So far, everything works except the if statement.到目前为止,除了if语句,一切正常。 (i tried the same if directly in the console and it worked, the problem occurs only in the fish_prompt). if直接在控制台中我尝试了同样的方法并且它有效,问题仅出现在fish_prompt中)。
Thanks谢谢

echo also returns a status, so $status is updated. echo还返回一个状态,因此 $status 被更新。

So if you are interested in the commandline's $status, you need to save that before doing anything else.因此,如果您对命令行的 $status 感兴趣,则需要先保存它,然后再执行其他任何操作。

Do this:做这个:

function fish_prompt
    # $status here is what the commandline returned,
    # save it so we can use it later
    set -l laststatus $status
    echo -n $USER
    echo -n "@"
    echo -n $hostname
    echo -n " "
    echo -n (prompt_pwd)
    echo -n (__fish_git_prompt)
    if [ $laststatus -ne 0 ] 
        echo -n " [$laststatus] "
    end 
    echo -n "> "
end 

This is also what fish's sample prompts do这也是fish的示例提示所做的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM