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
| Color | Code |
| Black | 0;30 |
| Blue | 0;34 |
| Green | 0;32 |
| Cyan | 0;36 |
| Red | 0;31 |
| Purple | 0;35 |
| Brown | 0;33 |
| Blue | 0;34 |
| Green | 0;32 |
| Cyan | 0;36 |
| Red | 0;31 |
| Purple | 0;35 |
| Brown | 0;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.
$ cdOR
$ vi .bash_profile
$ 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:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- My 10 UNIX Command Line Mistakes
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
- Email FAQ to a friend
- Download PDF version
- Printable version
- Comment RSS feed
- Last Updated: 12/7/07



{ 24 comments… read them below or add one }
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)
The way to costomise shell promt is very impressive. I WANT TO CHANGE BACKGROUND COLOR ALSO GIVE SOME IDEA.
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.
This topic was winderful. it is very easy to learn and simple
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?
I’ve looked at three other articles on colourising my bash prompt and this is the most clear and understandable of them all.
Thanks!
@Martin Norbäck
thank you. that’s the first time i see an explanation for that.
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
how to change promt(% to $) in unix ?
Just use this command:
That way, all you’re doing is adding the color code around your current PS1 variable (completely non-confusing and non-non-working).
> buu700 Says:
> Just use this command:
hey a****** – test the command before posting !
$ export PS1=”\e[0;31m[\u@\h \W]\$ \e[m ”
try this
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.
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
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
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
hey all.
What line do i need to use to get the text in green, whit a orange hostname?
thx in advanced,
penduleum
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
; )
this helped me. many thanks.
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.
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.
Replace for example Color 34 with 44.
(4)4 = Background
green_bg_color=”\E[0;37;42m”
What do 0 and 31 in 0;31m indicate? Is there a way of specifying color as RGB values?
For KSH …
1 example is :
FGWHITE=`echo “33[1;37m”`
echo “${FGWHITE}”
to back to normal :
NORMAL=`echo “33[m”`
echo “${NORMAL}”