Tag Archives: tweak

Bash prompt indicating return value

Lately I’ve fiddled a lot with installing virtuoso on some virtual machines and found myself repeatedly asking bash for the return value of the last command echo $?. I remembered this blog post by Gecko quite a while ago and tuned it a bit to my needs. My user prompt now looks like this (it’s a slightly modified version of the old gentoo defaults, that I prefer over the ubuntu defaults, which only remind you that you’re root with a # instead of a $):

The code for the user prompt:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m]u@h\[\033[00m]:\[\033[01;34m]w $(if [[ $? = 0 ]];then echo "\[\033[01;34m]";else echo "\[\033[01;31m]";fi)$ \[\033[00m]'

And the root prompt:

PS1='\[\033[01;31m]h\[\033[01;34m] W $(if [[ $? = 0 ]];then echo "\[\033[01;34m]";else echo "\[\033[01;31m]";fi)$\[\033[00m] '

And a small doc snippet you might want to include with your PS1 in your .bashrc, so you can still understand that cryptic stuff in a few days:

# h hostname
# u user
# w working dir (home == ~)
# $ a $ if UID == 0 else #
# A current 24-time: HH:MM
# \ a 
# [ begin, ] end of non-printing (control chars)
#
# colors: have to be surrounded by: '\[\033[' and 'm]' (without the ''. These literally are the left and right bracket!)
#  color codes are as follows, preceeded by a '0;' (dark) (default) or a '1;' (light)
#  FG and BG NULL: 00 resets
#  FG: 30 + ... 0: black, 1: red, 2: green, 3: yellow, 4: blue, 5: violet, 6: cyan, 7: white
#  BG: 40 + ... same