The 'ultimate' bash prompt

Of course, there is not one ultimate bash prompt that fits everyone, but I'm very happy with the following prompt.

Features:

  • user/hostname color coded for differnt hosts.
  • If running in a superuser shell, the # sign is red
  • the path is truncated to the last 25 characters

How to use it:

  • copy-paste the code below into your .bashrc or, better into .bash_aliases which won't be overwritten.
  • add the hostname of your servers after the respective color
  • restart the terminal and it should work right away.
bash_prompt_command() {
    # How many characters of the $PWD should be kept
    local pwdmaxlen=25
    # Indicate that there has been dir truncation
    local trunc_symbol=".."
    local dir=${PWD##*/}
    pwdmaxlen=$(( ( pwdmaxlen < ${#dir} ) ? ${#dir} : pwdmaxlen ))
    NEW_PWD=${PWD/#$HOME/\~}
    local pwdoffset=$(( ${#NEW_PWD} - pwdmaxlen ))
    if [ ${pwdoffset} -gt "0" ]
    then
        NEW_PWD=${NEW_PWD:$pwdoffset:$pwdmaxlen}
        NEW_PWD=${trunc_symbol}/${NEW_PWD#*/}
    fi
    PROMPT_ROOT='0m'
    if [ ${UID} -eq 0 ]; then
        PROMPT_ROOT='1;31m'
    fi
}
 
function setprompt()
{
    # host dependent prompt color, lookup table is below
    #default color is white
    hostnamecolor="01;37"
    while read cn cl hn
    do
        if [ "`hostname`" == "$hn" ] ; then
            HNCOL="\[\033[${cl}m\]"
    #        echo found $hostnamecolor
        fi
    done <<EOM
Black           0;30 
Blue            0;34 server1
Green           0;32
Cyan            0;36 server2
Red             0;31 
Purple          0;35 ws1
Brown           0;33 ws2
Light_Gray      0;37 
Dark_Gray       1;30 
Light_Blue      1;34 
Light_Green     1;32 
Light_Cyan      1;36 betelgeuze
Light_Red       1;31 
Light_Purple    1;35 
Yellow          1;33 
White           1;37 
EOM
 
    # ANSI color codes
    local RS="\[\033[0m\]"    # reset
    local HC="\[\033[1m\]"    # hicolor
    local UL="\[\033[4m\]"    # underline
    local INV="\[\033[7m\]"   # inverse background and foreground
    local FBLK="\[\033[30m\]" # foreground black
    local FRED="\[\033[31m\]" # foreground red
    local FGRN="\[\033[32m\]" # foreground green
    local FYEL="\[\033[33m\]" # foreground yellow
    local FBLE="\[\033[34m\]" # foreground blue
    local FMAG="\[\033[35m\]" # foreground magenta
    local FCYN="\[\033[36m\]" # foreground cyan
    local FWHT="\[\033[37m\]" # foreground white
    local BBLK="\[\033[40m\]" # background black
    local BRED="\[\033[41m\]" # background red
    local BGRN="\[\033[42m\]" # background green
    local BYEL="\[\033[43m\]" # background yellow
    local BBLE="\[\033[44m\]" # background blue
    local BMAG="\[\033[45m\]" # background magenta
    local BCYN="\[\033[46m\]" # background cyan
    local BWHT="\[\033[47m\]" # background white
 
#    PS1="$HC$HNCOL\u@\h$RS:\w]\$ "
#    PS1="$HC$HNCOL\h$RS:\w]\$ "
    PS1="$HC$HNCOL\u@\h$RS:\${NEW_PWD}]\[\e[\${PROMPT_ROOT}\]\\$ $RS"
}
 
setprompt
PROMPT_COMMAND=bash_prompt_command

~~LINKBACK~~

Enter your comment. Wiki syntax is allowed:
If you can't read the letters on the image, download this .wav file to get them read to you.