Fancy Terminal Prompt

By default, the terminal looks as follows on my Linux distribution:

However, if you are working a lot on the terminal, there are a lot of scripts and tricks available in the net that improve the information displayed in the terminal in many ways. For instance, since many many years, I have the following at the end of my ~/.bashrc:

# use a fancy prompt
PS1="$PS1 \`if [ \$? = 0 ]; then echo -e '\[\033[01;32m\]:-)';"
PS1="$PS1 else echo -e '\[\033[01;31m\]:-(' \$?; fi\`\[\033[00m\]"
PS1="$PS1 \$(__git_ps1 \"(%s)\") \$ "

Once you open a new terminal, then the appearance is as follows:

As you can see, now I have nice colors: The hostname is green, the folder is blue, the return value of the last executed command is a green :-) in case of success (exit code = 0), and a red :-( in case of errors (exit code != 0). In addition, the last part shows the current git branch (master). Showing the git branch is very useful especially if you are using arc a lot and work with many branches.

I am sure there are many more cool additions to the terminal. If you have some nice additions, please share – maybe also as a new blog?

9 thoughts on “Fancy Terminal Prompt

  1. The git branch thing is very useful (though where does __git_ps1 come from?) i have it done in a different way.

    One of the things i miss is that the shell is not “magic” and doesn’t realize i’ve changed to a different branch in another shell and updates itself to the new branch, so sometimes you have to press enter just to make sure that the branch is updated.

    Unfortunately i guess that’s unsolvable :/

  2. Actually, displaying the error code in the above example does not work. $0 will always be “1” because this is the return code of the failed comparison “[ $? = 0 ]”.

    To show the actual error code, $? must be stored, e.g:
    PS1=”$PS1 \`_E=\$?; if [ \$_E = 0 ]; then echo -e ‘\[\033[01;32m\]:-)’;”
    PS1=”$PS1 else echo -e ‘\[\033[01;31m\]:-(‘ \$_E; fi\`\[\033[00m\]”

    You can test by executing e.g. bash -c “exit 42”

    1. I experienced the same issue and copied your solution, but it produces the following error:

      syntax error near unexpected token `)’

  3. Need help on High Sierra. I am getting “-bash: __git_ps1: command not found” error. I did install bash-completion using brew and modified the ~/.bash_profile.

    Bash: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17)
    git: git version 2.14.3 (Apple Git-98)
    bash-completion: bash-completion: stable 1.3 (bottled)

    added line to ~/.bash_profile
    [ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion

  4. +1 for the zsh comments. After 20 years, I finally tried zsh because I wanted a couple of features I couldn’t get in bash. Just in one terminal at first, then another, then over a few months I switched all my terminal tabs, and finally changed my default shell to zsh. It’s a whole new world in a terminal.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top