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

by on October 6, 2006 · 54 comments· LAST UPDATED March 6, 2013

in , ,

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.
Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsBash
Estimated completion timeN/A

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


Where,

  • \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'
Where,

  • \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

ColorCode
Black0;30
Blue0;34
Green0;32
Cyan0;36
Red0;31
Purple0;35
Brown0;33
Blue0;34
Green0;32
Cyan0;36
Red0;31
Purple0;35
Brown0;33

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

OR
$ 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
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 54 comments… read them below or add one }

1 Martin Norbäck January 12, 2007 at 4:35 pm

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)

Reply

2 Charly August 13, 2012 at 9:00 am

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

Reply

3 sandeep Kumar July 7, 2007 at 5:33 am

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

Reply

4 T. King July 11, 2007 at 8:08 pm

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.

Reply

5 sanjith August 7, 2007 at 12:57 pm

This topic was winderful. it is very easy to learn and simple

Reply

6 Mark October 22, 2007 at 3:08 am

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?

Reply

7 Johu May 9, 2013 at 10:13 am

Read the first comment. I had the same issue.

Reply

8 Richard Burford December 7, 2007 at 2:21 pm

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

Thanks!

Reply

9 ramigs December 24, 2007 at 1:35 am

@Martin Norbäck

thank you. that’s the first time i see an explanation for that.

Reply

10 Ruben December 30, 2007 at 6:14 pm

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.

Regards, Ruben.ie

Reply

11 ramana March 4, 2008 at 7:07 am

how to change promt(% to $) in unix ?

Reply

12 buu700 June 13, 2008 at 6:47 pm

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

Reply

13 matelot July 7, 2008 at 3:15 am

> buu700 Says:
> Just use this command:

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

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

Reply

14 R.I.M. Rizme September 4, 2008 at 3:24 am

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

try this

Reply

15 MacUser December 3, 2008 at 1:41 am

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.

Reply

16 strnik January 13, 2009 at 1:07 pm

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
else
PS1=’[\u@\h:\w]\$ ‘
fi

Reply

17 Andres January 31, 2009 at 1:56 am

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

Reply

18 Amira February 18, 2009 at 6:47 am

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

Reply

19 penduleum February 21, 2009 at 7:35 pm

hey all.

What line do i need to use to get the text in green, whit a orange hostname?

thx in advanced,

penduleum

Reply

20 Lionel.rpm May 8, 2009 at 12:46 am

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

; )

Reply

21 Purav May 15, 2009 at 2:04 pm

this helped me. many thanks.

Reply

22 Black_Ps` June 11, 2009 at 3:35 am

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.

Thanks.

Reply

23 Saurabh August 13, 2009 at 7:33 am

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.

Reply

24 Sassy Grrrl June 9, 2011 at 2:52 pm

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

Reply

25 oinkboing September 16, 2009 at 12:01 pm

Replace for example Color 34 with 44.
(4)4 = Background
green_bg_color=”\E[0;37;42m”

Reply

26 Jigsaw September 27, 2009 at 7:55 pm

What do 0 and 31 in 0;31m indicate? Is there a way of specifying color as RGB values?

Reply

27 ss February 8, 2010 at 10:38 pm

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

Reply

28 Tynged April 14, 2010 at 7:57 pm

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.

Reply

29 Matt June 17, 2011 at 7:20 pm

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\] " 

Reply

30 Cq November 11, 2011 at 5:20 pm

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?

Reply

31 Richie February 8, 2012 at 5:10 pm

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.

Reply

32 Nat Harward February 8, 2012 at 11:17 pm

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

Reply

33 maseny May 3, 2010 at 3:44 pm

i want to modify the way dirctory a creaded in bash

Reply

34 aciD May 16, 2010 at 1:26 pm

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

Reply

35 Stefan Lasiewski June 8, 2010 at 4:51 am

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 http://mywiki.wooledge.org/BashFAQ/037?highlight=%28ps1%29 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\]\$ ‘

Reply

36 Fabiano PS May 25, 2011 at 8:14 pm

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

Reply

37 Stefan Lasiewski June 8, 2010 at 5:55 pm

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 http://mywiki.wooledge.org/BashFAQ/037?highlight=%28ps1%29 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\]\$ ‘

Reply

38 Christian Haarmann June 18, 2010 at 3:04 pm

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”

Reply

39 Stefan Lasiewski June 24, 2010 at 12:20 am

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
xterm-color
$ echo “$(tput bold)BOLD$(tput sgr0)”
BOLD

Reply

40 Michael August 2, 2010 at 4:01 pm

Can we change the colors of terminal that is appearing on the booting?

Reply

41 Albert October 25, 2010 at 1:44 pm

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

Reply

42 Rohit February 15, 2011 at 8:22 pm

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…

Reply

43 Jin May 22, 2011 at 6:19 pm

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

My PS1 is:

PS1=’\e[0;31m[\u@h \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~

Reply

44 Jin May 22, 2011 at 6:26 pm

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\][\u@h \w]\$ \[\e[m\]‘

Reply

45 Matt June 17, 2011 at 6:45 pm

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

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

When I used this it works perfectly.

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

Reply

46 sangdol February 25, 2012 at 3:19 pm

@Jin Thanks a lot! Author should’ve write about that.

Reply

47 Nat Harward April 1, 2011 at 10:42 pm

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.

Reply

48 Nat Harward May 28, 2011 at 3:04 am

This inspired me to create Baven [https://github.com/nharward/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 https://github.com/nharward/baven/blob/master/bin/samples/ansi-colors.bash for a working example.

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

Reply

49 Matt June 17, 2011 at 7:41 pm

These 2 methods work best for me.

export PS1='\[33[0;32m\][\u@\h \W]\$\[33[0m\] '
export PS1="\[\e[0;31m\][\u@\h \w]\$ \[\e[m\]"

These are some good references.
http://www.ibm.com/developerworks/linux/library/l-tip-prompt/
http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html

Reply

50 Thibaut August 5, 2011 at 8:35 am

That was helpful, thanks.

Reply

51 JJ November 27, 2011 at 9:03 am

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”

Reply

52 pradeep July 3, 2012 at 9:17 am

Thanks, working fine.

Reply

53 g December 7, 2012 at 7:08 am

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

Reply

54 splatter September 17, 2013 at 9:34 pm

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!

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: