Informative Bash Prompt

Posted: 1 Jun 2005

Informative bash prompt

The normal bash prompt in SuSE Linux contains the username, the machine name and the working directory. I don't need much more, but I like to have it clearer visible (colored). I also want to be informed if an application exited with exitcode > 0. Additionally, it's very useful to have username, hostname and the working directory in the xterm title.

This is my prompt:

cb@tux:/tmp> false
   [1]   cb@tux:/tmp> su
Password:
root@tux:/tmp>

You can see that the false command exited with status 1 as expected. You can also see that root sessions are highlighted separately.

How does this work?

I changed my prompt ($PS1) for the described features. The most important change is displaying the exitcodes if they are not zero. My /etc/bash.bashrc.local contains:

# PS1version=' [e[46m]8.1[e[0m]'
PS1error='$( ret=$? ; test $ret -gt 0 && echo "[e[41;93m]   [$ret]   [e[0m]" )'
PS1user="$( test `whoami` == root && echo '[e[101m]' )u[e[0m]"
PS1color='[e[1;37;44m]' # color of working directory
PS1="$PS1error$PS1user@h:$PS1colorw[e[0m]$PS1version> "
tty | grep pts > /dev/null && PS1="$PS1[e]0;w - u@ha]";
export PS1

What do all those commands and variables do? I'll explain each step.

  • The (commented out) line PS1version is useful in chroot environments and helps me in looking through the different systems. If activated, the prompt contains the additional text 8.1 with cyan background.
  • PS1error prints the exitcode and its colored formatting - except if it was zero (= success). In this case, nothing is printed.
    The variable $ret is necessary because test changes $?.
  • PS1user displays the current username - normally unformatted, only root gets a light red background.
    The usage of whoami has the advantage of always telling the correct username. The bash-internal $USER variable is only correct with su - (or real logins), but not when just using su.
    By the way, $PS1user is expanded directly when parsing bash.bashrc.local - that means whoami is only called once in a session.
  • PS1color contains the color codes for highlighing of the working directory. The advantage of putting this in its own variable is that it can be changed easier.
    Tip: I use different colors on several machines and can recognice the xterms by color ;-)
  • The line PS1=... composes the "real" prompt. It puts all the PS1... variables together and adds some more details.
    Important: $PS1error is still not expanded here. It is evaluated each time the promt is shown.
  • Using tty | grep pts ... puts the working directory, username and host into a xterm's title bar - if you are in one. Otherwise, it changes nothing.
  • Finally the variable $PS1 gets exported (export $PS1) to be really useable.

Reading tips about this topic: man console_codes explains all the colorful parts of my prompt, man bash explains all about the promt in general. Additionally, the bash prompt howto is worth reading.

Available colors

for i in `seq 1 7 ; seq 30 48 ; seq 90 107 ` ; do 
    echo -e "e[${i}mteste[0m$i" 
done

shows a list of available colors.

The colors 90-107 are not documented in man console_codes. Before using them, use the above loop to test if they are supported on your system.



Informacja z serwisu http://www.djack.com.pl