BASH Shell change the color of my shell prompt under Linux or UNIX

by Vivek Gite on October 6, 2006 · 42 comments

Q. How do I change the color of my shell prompt under Linux ?

A. You can change the color of your shell prompt to impress your friend or to make your own life quite easy while working at command prompt.

In the Linux default shell is BASH.

Your current prompt setting is stored in PS1 shell variable. 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 echo command to display current BASH prompt:
$ echo $PS1Output:

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

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

  • \u: Display the current username
  • \h: Display the hostname
  • \W: Print the current working directory

Task: Modify current BASH prompt

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

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 is your shell prompt
  • \e[m Stop color scheme

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

List of Color code

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

Replace digit 0 with 1 to get light color version.

Task: How to make the prompt setting permanent

Your new shell prompt setting is temporary i.e. when you logout setting will be lost. To have it set everytime you login to your workstation add above export command to your .bash_profile file or .bashrc file.
$ cd
$ vi .bash_profile
OR
$ vi .bashrc
Append export line:
export PS1="\e[0;31m[\u@\h \W]\$ \e[m"

Save and close the file.

tput command

You can also use tput command. For example display RED prompt use tput as follows:
export PS1="\[$(tput setaf 1)\]\u@\h:\w $ \[$(tput sgr0)\]"

handy tput commands

  • tput bold - Bold effect
  • tput rev - Display inverse colors
  • tput sgr0 - Reset everything
  • tput setaf {CODE}- Set foreground color, see color {CODE} below
  • tput setab {CODE}- Set background color, see color {CODE} below

Colors {code} code for tput command

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

Read the man page of bash and tput command for more information.

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 42 comments… read them below or add one }

1 Martin Norbäck January 12, 2007

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 sandeep Kumar July 7, 2007

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

Reply

3 T. King July 11, 2007

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

4 sanjith August 7, 2007

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

Reply

5 Mark October 22, 2007

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

6 Richard Burford December 7, 2007

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

7 ramigs December 24, 2007

@Martin Norbäck

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

Reply

8 Ruben December 30, 2007

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

9 ramana March 4, 2008

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

Reply

10 buu700 June 13, 2008

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

11 matelot July 7, 2008

> buu700 Says:
> Just use this command:

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

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

Reply

12 R.I.M. Rizme September 4, 2008

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

try this

Reply

13 MacUser December 3, 2008

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

14 strnik January 13, 2009

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

15 Andres January 31, 2009

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

16 Amira February 18, 2009

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

17 penduleum February 21, 2009

hey all.

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

thx in advanced,

penduleum

Reply

18 Lionel.rpm May 8, 2009

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

19 Purav May 15, 2009

this helped me. many thanks.

Reply

20 Black_Ps` June 11, 2009

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

21 Saurabh August 13, 2009

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

22 Sassy Grrrl June 9, 2011

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

23 oinkboing September 16, 2009

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

Reply

24 Jigsaw September 27, 2009

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

Reply

25 ss February 8, 2010

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

Reply

26 maseny May 3, 2010

i want to modify the way dirctory a creaded in bash

Reply

27 aciD May 16, 2010

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

28 Stefan Lasiewski June 8, 2010

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

29 Fabiano PS May 25, 2011

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

30 Stefan Lasiewski June 8, 2010

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

31 Christian Haarmann June 18, 2010

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

32 Stefan Lasiewski June 24, 2010

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

33 Michael August 2, 2010

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

Reply

34 Rohit February 15, 2011

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

35 Jin May 22, 2011

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

36 Jin May 22, 2011

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

37 Nat Harward April 1, 2011

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

38 Nat Harward May 28, 2011

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

39 Thibaut August 5, 2011

That was helpful, thanks.

Reply

40 JJ November 27, 2011

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

41 Tynged April 14, 2010

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

42 Cq November 11, 2011

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

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 14 + 13 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the simple math so we know that you are a human and not a script.




Previous post:

Next post: