BASH Shell: Change The Color of My Shell Prompt Under Linux or UNIX

Posted on in Categories , , last updated March 6, 2013

How do I change the color of my shell prompt under Linux or Unix operating systems?

You can change the color of your shell prompt to impress your friend or to make your own life quite easy while working at the command prompt. BASH shell is the default under Linux and Apple OS X. Your current prompt setting is stored in a shell variable called PS1. There are other variables too, like PS2, PS3 and PS4.

Bash displays the primary prompt PS1 when it is ready to read a command, and the secondary prompt PS2 when it needs more input to complete a command. Bash allows these prompt strings to be customized by inserting a number of backslash-escaped special characters.

Task: Display current BASH prompt (PS1)

Use the echo command to display current BASH prompt:
$ echo $PS1
Sample outputs:

[\\u@\h \\W]\\$

Here is another output from my Debian based system:
$ echo $PS1
Sample outputs:

\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$

By default the command prompt is set to [\u@\h \W]\$. The backslash-escaped special characters are decoded as follows:

  • \u: Display the current username .
  • \h: Display the hostname
  • \W: Print the base of current working directory.
  • \$: Display # (indicates root user) if the effective UID is 0, otherwise display a $.

Task: Modify current BASH prompt

Use the export command to setup a new shell prompt:
$ export PS1="[\\u@\\H \\W \\@]\\$"
Sample outputs:

Fig.01: New prompt in action
Fig.01: New prompt in action


  • \H: Display FQDN hostname.
  • \@: Display current time in 12-hour am/pm format

Task: Add colors to the prompt

To add colors to the shell prompt use the following export command syntax:
'\e[x;ym $PS1 \e[m'

  • \e[ : Start color scheme.
  • x;y : Color pair to use (x;y)
  • $PS1 : Your shell prompt variable.
  • \e[m : Stop color scheme.

To set a red color prompt, type the following command:
$ export PS1="\e[0;31m[\u@\h \W]\$ \e[m "
Sample outputs:

Fig.02: Adding the colors to the prompt
Fig.02: Adding the colors to the prompt

A list of color codes


Note: You need to replace digit 0 with 1 to get light color version.

Task: How do I make the prompt setting permanent?

Your new shell prompt setting set by $PS1 is temporary i.e. when you logout setting will be lost. To have it set every time you login to your workstation add above export command to your $HOME/.bash_profile file or $HOME/.bashrc file.
$ cd
$ vi .bash_profile

$ vi $HOME/.bashrc
Append the following line:
export PS1="\e[0;31m[\u@\h \W]\$ \e[m"
Save and close the file.

Say hello to tput command

You can also use tput command to set terminal and modify the prompt settings. For example, to display RED color prompt using a tput:
export PS1="\[$(tput setaf 1)\]\u@\h:\w $ \[$(tput sgr0)\]"

A list of handy tput command line options

  • tput bold – Bold effect
  • tput rev – Display inverse colors
  • tput sgr0 – Reset everything
  • tput setaf {CODE}– Set foreground color, see color {CODE} table below for more information.
  • tput setab {CODE}– Set background color, see color {CODE} table below for more information.

Various color codes for the tput command

Color {code}Color
0 Black
1 Red
2 Green
3 Yellow
4 Blue
5 Magenta
6 Cyan
7 White
Recommend readings

65 comment

  1. you also need to put \[ and \] around any color codes so that bash does not take them into account when calculating line wraps. Also you can make use of the tput command to have this work in any terminal as long as the TERM is set correctly. For instance $(tput setaf 1) and $(tput sgr0)

    1. Thx Martin
      The \[ and \] around color codes are really useful.
      When going through the history, the size of the prompt was incorrectly calculated and I had characters from a previous command that would add to the prompt. Thanks to you, this error is now corrected.
      As an example here is my PS1:
      PS1=’\[\e[0;32m\]\u@\h:\w\$ \[\e[0m\]’

    2. Thank you Martin. I’ve spent ages trying to fix an annoying behavior in my terminal when lines wrapped over to the next line, and/or I hit backspace in some situations.

      I’d not wrapped my color codes in `\[` or `\]`, and now they’re fixed! Thank you!

  2. The way to costomise shell promt is very impressive. I WANT TO CHANGE BACKGROUND COLOR ALSO GIVE SOME IDEA.

  3. Using the first example did cause the first line wrap to simple start back over the first line again, creating issues when entering a two-line command. I’m sure using #1’s suggestion about using \[ and \] around color codes would work, but I settled on using the tput instead, which fixed the line-wrap issue.

  4. Can anyone tell me why after adding the color codes, once text has gone to the next line, i cant backspace back to the 1st line, it just stops at the far left of second line?

  5. I’ve looked at three other articles on colourising my bash prompt and this is the most clear and understandable of them all.


  6. After read some articles here is my way:

    # ==== alias for colors ========
    BLACK="tput setf 0"
    BLUE="tput setf 1"
    GREEN="tput setf 2"
    CYAN="tput setf 3"
    RED="tput setf 4"
    MAGENTA="tput setf 5"
    YELLOW="tput setf 6"
    WHITE="tput setf 7"
    RETURN="tput sgr0"
    BOLD="tput bold"
    REV="tput rev"
    # Some examples of use
    PS1="`$REV``$RED`[$netip `$BLUE`/\W]#`$RETURN` "
    PS1="`$REV``$BLACK`[`$RED`$netip `$BLUE`\w`$BLACK`]#`$RETURN` "
    PS1="`$REV`[`$RED`$netip `$BLUE`\w]#`$RETURN` "
    PS1="`$REV``$RED`[$netip `$BLUE`\w]#`$RETURN` "
    # where netip is 
    netip=`/sbin/ifconfig eth0 | awk -F: '/inet addr/ {print $2}' | awk '{print $1}'  | cut -b 11-`;

    I hope someone find it useful.


  7. Just use this command:

    export PS1="\e[0;31m$(echo PS1)\e[m"

    That way, all you’re doing is adding the color code around your current PS1 variable (completely non-confusing and non-non-working).

  8. > buu700 Says:
    > Just use this command:

    hey a****** – test the command before posting !

    export PS1="\e[0;31m$(echo $PS1)\e[m"
  9. On a mac, the tget bold command doesn’t use the bold color from your terminal preferences. But you can get around this annoyance by using

    \[\e[1m\] \[e[m\]

    Which will send the ASCII bold character and terminal respects that.

  10. all examples above did not work properly in bash on Solaris 10 for me, so this is what I ended up with :

    if [ `hostname|cut -c -11` == lonlnddebtp ]; then
    PS1=’\[\e[1;31m\][\u@\h:\w]\$\[\e[0m\] ‘ # PROD, red color
    elif [ `hostname|cut -c -11,13-` == lonlnddebtd-z1 ]; then
    PS1=’\[\e[2;32m\][\u@\h:\w]\$\[\e[0m\] ‘ # UAT, green color
    PS1='[\u@\h:\w]\$ ‘

  11. I use this to set my prompt
    set prompt = “%B%{33[31m%}%m %{33[37m%}%B%// \n”

    It placed the machine name in orange color, and the path in white. It is separated by a space, so I can easily select the path by double clicking it…and I also included a / to allow me to append anything else afterwards

  12. hi,..
    sry but what if i want to change the colours of the command it self
    like (ls -l /etc/passwd )i want for example(ls) in red and (-l) with blue and (the name of file) with green

  13. export PS1=”\[$(tput setaf 1)\]\u@\h:\w $ \[$(tput sgr0)\]”

    \u@\h:\w $ \[ Why would i use $ for root?

    means? \u@\h:\w\\$ \[ —> this is the best way.

    Nice work Vivek

    ; )

  14. export PS1=”\[[33[01;32m\]\u@\h\[33[01;34m\] \W]\]#” how can i execute this command from a .sh script or .c and make it work,can somebody help.


  15. Hi,
    I am using cygwin. I could not find the files .bash_profile file or .bashrc file anywhere in installation directory. I want to change color parmanent.

    1. Files that start with a . are invisible if you just run a regular ls command you can’t see them so run ls -la instead. Also, even when you don’t see them you can still edit them using your favorite editor by just making sure that when you load the file you use the dot. For example: emacs .bash_rc

  16. For KSH …
    1 example is :
    FGWHITE=`echo “33[1;37m”`
    echo “${FGWHITE}”
    to back to normal :
    NORMAL=`echo “33[m”`
    echo “${NORMAL}”

  17. I figured out how to color parts separately. Here’s what I used:

    export PS1=”\[\e[0;33m\][\[\e[0;32m\]\u\[\e[0;33m\]@\h:\[\e[0;39m\]\w\[\e[0;33m\]]\$\[\e[0m\] ”

    To get the neon colors in the video linked above I assume you’d have to change the precise display colors used by your ssh client. So, using PuTTY for example, you’d have to modify the RGB values of each ANSI color in the Window->Colours menu.

    1. Thanks for this. Don’t forget to change the curly quotes ”” to regular straight quotes “”. The shell only know knows how to use regular quotes. So it needs to look like this.

      export PS1="\[\e[0;33m\][\[\e[0;32m\]\u\[\e[0;33m\]@\h:\[\e[0;39m\]\w\[\e[0;33m\]]\$\[\e[0m\] " 

    2. I do not see the answer to an earlier questio: Is it possible to simply reverse everything on the screen, including the normal outputs of utilities and scripts. By default the background is black with foreground white. I wish to reverse this everywhere to reduce reflections on the screen. Any way?

      1. You will want to modify the terminal parameters. If you are using xterm or putty, etc, change it there. If you are using true terminal no gui then this i am afraid may not be fully possible.

      2. Just type: tput rev

        However, if you run any program that manipulates the terminal settings (like running “ls” with the “–color” option, for example) it will probably get reset at that point. To make it permanent you’d need to adjust the settings of whatever your terminal program is (as Richie says below).

  18. The best PS1 :P :

    PS1=’\[33[1;33m\]\u\[33[1;37m\]@\[33[1;32m\]\h\[33[1;37m\]: \[33[1;31m\]\w\n\[33[1;36m\]\$ \[33[0m\]’

    (dont forget to change it in .bashrc in all home folders include /root ;) )

  19. If you use `export PS1=”\[$(tput setaf 1)\]\u@\h:\w $ \[$(tput sgr0)\]”`, then won’t this be forking ‘tput’ every time PS1 is set? The BashFAQ at suggests doing this instead:

    # Bash
    red=$(tput setaf 1)
    green=$(tput setaf 2)
    blue=$(tput setaf 4)
    reset=$(tput sgr0)
    PS1=’\[$red\]\u\[$reset\]@\[$green\]\h\[$reset\]:\[$blue\]\w\[$reset\]\$ ‘

    1. Thanks! Finally set with one scheme that does not break my lines using CTRL+R ou checking history!

      green=$(tput setaf 2)
      blue=$(tput setaf 4)
      reset=$(tput sgr0)
      PS1=’\[$green\]\u\[$reset\] \[$blue\]\w\[$reset\] \$ ‘

  20. Vivek, this is great stuff as always. However, if PS1 will run the $(tput foo) command, won’t it fork the tput commands every time PS1 is displayed?

    The suggestion at is to store the tput output in environment variables, and use those variables:

    # Bash
    red=$(tput setaf 1)
    green=$(tput setaf 2)
    blue=$(tput setaf 4)
    reset=$(tput sgr0)
    PS1=’\[$red\]\u\[$reset\]@\[$green\]\h\[$reset\]:\[$blue\]\w\[$reset\]\$ ‘

  21. You can give three arguments, not only two!
    “\e[x,y,zm” with x=brightness, y=foreground, z=background. For example:
    “\e[0;33;40m” shows dark (=0) yellow (=33) characters on black (=40) background

    The alternate way using “tput” doesn’t work in my cygwin and prints the error message:
    tput: unknown Terminal “xterm-color”

  22. Sadly, tput on FreeBSD 7.x doesn’t support these fancy options. The tput man page has no mention of options like ‘bold’ and ‘color’. This works on Linux.

    $ echo $TERM
    $ echo “$(tput bold)BOLD$(tput sgr0)”

  23. Hi, i think there is a little mistake.
    Tried to add the initial and ending colors mark and being unsuccessful, i’ve found that the complete color start mark is \e[x;ym
    Note the final m, which in my opinion is not clearly explained above.

    Great tutorial anyway, thx! ;)

  24. I changed the color of my Linux prompt successfully. But now the commands that I run do not carry forward to the same line. If I have a tar -zcvf command with 5 or 6 log files, the command rolls over to the same line. Its so confusing that I cant see the first part of my own command. Is there a way out of this?

    Thanks guys…

    1. Yes, I have exactly the same problem with my Mac OS X:

      My PS1 is:

      PS1=’\e[0;31m[\[email protected] \w]\$ \e[m’

      And then, if my command line is really long, it wraps around and overwrites the prompt; and if the command line is super long and it wraps around the second time, this time it would correctly push the terminal output one line up and start to print in the new line.

      Help please~

      1. Duh…10 seconds after I post the question, I figured out. Basically any sequence like “\e[32m” needs to be enclosed by “\[” and “\]”, just like Tynged mentioned below.

        PS1=’\[\e[0;31m\][\[email protected] \w]\$ \[\e[m\]’

        1. I had to use double quotes “” instead of back quotes ’’ . When I tried back quotes I got this message.

          $ export PS1=’\[\e[0;31m\][\[email protected] \w]\$ \[\e[m\]‘
          bash: 31m][[email protected]: command not found…
          Failed to search for file: Invalid input passed to daemon: char ‘]’ in text!

          When I used this it works perfectly.

          export PS1=”\[\e[0;31m\][\u@\h \w]\$ \[\e[m\]”

  25. This thread is old but if anyone is still reading it, you probably want to change “tput” with “tput -T${TERM:-dumb}” so that if by some chance you are *not* in a nice TTY you won’t get garbage characters.

  26. This inspired me to create Baven [] by not ever wanting to write “tput” ever again :)

    It is a plugin loader for BASH allowing to dynamically load BASH functions, over the network if needed (similar to Maven plugins). One is there specifically to handle ANSI colors, see for a working example.

    If anyone has suggestions for making it better, I’m all ears…

  27. I just want to thank you for having a simple, concise explanation after each bash expression.

    Too often guides simply say: “Paste X at the bottom of file Y. Got that? Ok, GTFO”

  28. On Ubuntu 12.04.1 LTS (precise, xubuntu) bash uses ~/.bashrc
    This file has settings to modify the prompt and colors.
    By default the colors are off and it explains why:
    # uncomment for a colored prompt, if the terminal has the capability; turned
    # off by default to not distract the user: the focus in a terminal window
    # should be on the output of commands, not on the prompt
    # force_color_prompt=yes
    So to turn the colors on: remove the octothorpe in front of force_color_prompt

  29. THANK YOU! That prompt over write was driving me crazy, and I had a hard time finding anything on it until today I finally plugged in the right terms in google. You guys rock!

  30. Instead of this use setterm e.g. setterm -back (colour) then type clear to change the entire background. to change text do setterm -fore (colour)

  31. Thanks for the nice docs.

    In “A list of color codes”, each foreground color (0;30 to 0;37) is listed twice. It would be nice if they were in numeric order, followd by the list of background colors (0;40 to 0;47).

  32. Man, edit this article to include the \[ and \] enclosing braces when the color codes are! I was in the dumps trying to figure why the hell my terminal doesnt wrap long lines!
    A big -1 !!

Leave a Comment