Hack 37. Change foreground color of the prompt

Display prompt in blue color, along with username, host and current directory information

$ export PS1="\e[0;34m\u@\h \w> \e[m "

[Note: This is for light blue prompt]

$ export PS1="\e[1;34m\u@\h \w> \e[m "

[Note: This is for dark blue prompt]
  • \e[ – Indicates the beginning of color prompt
  • x;ym – Indicates color code. Use the color code values mentioned below.
  • \e[m – indicates the end of color prompt

Color Code Table:

  • Black 0;30
  • Blue 0;34
  • Green 0;32
  • Cyan 0;36
  • Red 0;31
  • Purple 0;35
  • Brown 0;33

[Note: Replace 0 with 1 for dark color]

Make the color change permanent by adding the following lines your ~/.bash_profile or ~/.bashrc

$ vi ~/.bash_profile

STARTCOLOR='\e[0;34m';
ENDCOLOR="\e[0m"
export PS1="$STARTCOLOR\u@\h \w> $ENDCOLOR"

Comments on this entry are closed.

  • Motherdawg August 7, 2010, 3:09 pm

    Hi guys,

    I got a good one!
    I colorized my prompt as follow; Green for normal user, Red for root:
    Root .bashrc:
    PS1=”\e[0;31m[\t][\u@\h:\w][\!]\\$ \e[m”
    User .bashrc:
    PS1=”\e[0;37m[\t][\u@\h:\w][\!]\\$ \e[m”
    Which gives:
    [17:53:18][root@T42:/home/netweezurd][93]#
    in technicolor…

    When the command I’m typing is longer than the screen, the cursor returns to the left margin but not to a new line anymore, if I continue typing it kind of overwrite the prompt on the same line. If I backup it does jump one line up and ends up on the line of the previous command… in emptiness… unworkable !

    Right now I’m putting nix on a usbkey and it’s giving me:
    [17:53:18][root@T42:/home/netweezurd][93]# ^C
    [17:53:18][root@T42:/home/netweezurd][93]# livecd-iso-to-disk –overlay-size-mb 512 –format –reset-mbr Fedora-13-i686-Live-Security/Fedora-13-i686-Live-Security.iso /dev/sdb1weezurd][93]#

    I dunno if you can figure it, the weezurd part at the end is on the same line, the cursor just wrapped around.

    My normal no color prompt does not do that.
    PS1=”[\t][\u@\h:\w][\!]\\$ ”

    Linux T42.mobile1 2.6.33.6-147.2.4.fc13.i686 #1 SMP Fri Jul 23 17:27:40 UTC 2010 i686 i686 i386 GNU/Linux

    Any clues ???

  • Motherdawg August 7, 2010, 11:33 pm

    I got it to work… with some help from penguiniator & David the H. over at linuxquestions.org.

    I think your missing escape sequences:
    This work ok: PS1=”\[\e[0;31m\][\t][\u@\h:\w][\!]\\$ \[\e[m\]”
    Compared to: PS1=”\e[0;31m[\t][\u@\h:\w][\!]\\$ \e[m”

    Kewl
    Cya & thx.