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

Posted on in Categories , , last updated September 25, 2017

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:

[\\[email protected]\h \\W]\\$

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

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

By default the command prompt is set to [\[email protected]\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

Before you modify settings save your old prompt using the following command:
So if you messed up, you can switch back easily using the following syntax:
Use the export command to setup a new shell prompt:
$ export PS1="[\\[email protected]\\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[\[email protected]\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[\[email protected]\h \W]\$ \e[m"
Save and close the file.
Add the following command in ~/.bashrc:

export PS1="\[\e[32m\][\[\e[m\]\[\e[31m\]\u\[\e[m\]\[\e[33m\]@\[\e[m\]\[\e[32m\]\h\[\e[m\]:\[\e[36m\]\w\[\e[m\]\[\e[32m\]]\[\e[m\]\[\e[32;47m\]\\$\[\e[m\] "

You will get prompt as follows

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)\]\[email protected]\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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Share this on (or read 66 comments/add one below):

66 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\]\[email protected]\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. 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.

  3. 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.


  4. 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).

  5. 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.

  6. 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

    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

  7. 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).

  8. 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 ;) )

    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\] \$ ‘

  9. 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\]\$ ‘

  10. 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”

  11. 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)”

  12. 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! ;)

  13. 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. 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\][\[email protected]\h \w]\$ \[\e[m\]”

  14. 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.

  15. 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…

  16. 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

  17. 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).

  18. 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 !!

  19. Please edit this command:
    export PS1=”\e[0;31m[\[email protected]\h \W]\$ \e[m ”
    to contain ‘\[‘ and ‘\]’ allowing for correct placement of the cursor for multiple line commands.

    Following your example led me to have to having issues with two line commands for the last month until I finally figured out what the problem was and how to fix it.

Leave a Comment