Hack 39. Display multiple colors in the prompt

by Ramesh

You can also display multiple colors in the same prompt. Add the following function to your ~/.bash_profile

  1. function prompt {
  2. local BLUE="\[\033[0;34m\]"
  3. local DARK_BLUE="\[\033[1;34m\]”
  4. local RED=”\[\033[0;31m\]”
  5. local DARK_RED=”\[\033[1;31m\]”
  6. local NO_COLOR=”\[\033[0m\]”
  7. case $TERM in
  8. xterm*|rxvt*)
  9. TITLEBAR=’\[\033]0;\u@\h:\w\007\]’
  10. ;;
  11. *)
  12. TITLEBAR=”"
  13. ;;
  14. esac
  15. PS1=”\u@\h [\t]>
  16. PS1=”${TITLEBAR}\
  17. $BLUE\u@\h $RED[\t]>$NO_COLOR
  18. PS2=’continue->
  19. PS4=’$0.$LINENO+
  20. }

You can re-login for the changes to take effect or source the .bash_profile as shown below.

  1. $. ./.bash_profile
  2.  
  3. $ prompt
  4.  
  5. ramesh@dev-db [13:02:13]>