Prompt me, Bash! Pronto!

While searching for a promt command for bash to trimm long pwds (current working directory) I stumbled uppon “Blog.ubrious“. After a little hacking I adopted it to work for linux bash. It also cuts off leading characters when pwd is longer than 20 characters with build-in commands only. Plus I opted to use a ‘@’ as dilimeter between user name and host name so to rather have ssh style. Here is what I came up with:

PROMPT_COMMAND='PS1="\[33[0;33m\][\!]\`if [[ \$? = "0" ]]; then echo "\\[\33[32m\\]"; else echo "\\[\33[31m\\]"; fi\`[\u'@'\h: \`if [[ ${#PWD} > 20 ]]; then echo ${PWD:$((${#PWD}-20))}; else echo "\\w"; fi\`]\$\[33[0m\] "; echo -ne "33]0;`hostname -s`:`pwd`07"'

If wordpress does mess up the code (which I hope does not happen) please visit the original blogger’s post since he was able to upload a text file (I’m not, unfortunatelly).Here is a small list on what it can do for you and a little demo:

  1. Changes to red if the last command didn’t have a return code of 0
  2. Includes your bash history # for easy history command repeating. Follow the link to find out how to use !!, !$ and !number.
  3. Makes sure path length doesn’t exceed 30 regardless of what the base name is (bit after last fwd slash).

Bash prompt trimming

The only small hassle about it is even though it uses \w it wouldn’t abreveate my home directory as ‘~’ for me.

Note 2008/05/14: If you want to use this promt command with screen, too, make sure to test $TERM for ‘screen’. For example mine reads:

case "$TERM" in
xterm*|rxvt*|screen*)
     PROMPT_COMMAND=...