cboltz.de

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:$PS1color\w\[\e[0m\]$PS1version> " tty | grep pts > /dev/null && PS1="$PS1\[\e]0;\w - \u@\h\a\]"; export PS1

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

Reading tips about this topic: man console_codes explains all the colorful parts of my prompt, man bash explains all about the prompt 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 "$i \e[${i}mtest\e[0m" 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.

last update: 20.2.2005