Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.
What I want : 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.
So far, everything works except the if statement. (i tried the same if directly in the console and it worked, the problem occurs only in the fish_prompt) .
Thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

echo also returns a status, so $status is updated.

So if you are interested in the commandline’s $status, you need to save that before doing anything else.

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading