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. Let us see how to change the color of shell prompt on a Linux or Unix system when using bash.
Tutorial details | |
---|---|
Difficulty | Easy (rss) |
Root privileges | No |
Requirements | Bash |
Time | 5m |
Task: Displaying current BASH prompt (PS1)
Use the echo command/printf command to display current BASH prompt settings:
$ echo "$PS1"
## OR ##
$ printf "%s\n" "$PS1"
Here is what I see:
[\\u@\h \\W]\\$
Here is another output from my Debian based system:
$ echo $PS1
Ubuntu/Debian based Linux distro displayed the following:
\[\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: Modifing current BASH prompt
Before you modify settings save your old prompt using the following command:
oldps1="$PS1"
So if you messed up, you can switch back easily using the following syntax:
PS1="$oldps1"
Use the export command to setup a new shell prompt:
$ export PS1="[\\u@\\H \\W \\@]\\$ "
Fig.01: New prompt in action
Where,
- \H: Display FQDN (fully qualified domain name) hostname.
- \@: Display current time in 12-hour am/pm format.
Task: Adding 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.
Change the color of shell prompt by setting the PS1
To set a red color prompt, type the following export command:
$ export PS1="\e[0;31m[\u@\h \W]\$ \e[m "
Fig.02: Adding the colors to the prompt
A list of color codes
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 |
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 in vim/vi.
Bash Change The Color of Shell Prompt Example
Add the following command in ~/.bashrc:
# let us setup prompt 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:
Here is another example that include exit status of command as well:
# #\u - user name #\h - short hostname #\W - current working dir #\? - exit status of the command export PS1="{\[\e[32m\]\u\[\e[m\]@\[\e[36m\]\h\[\e[m\]:\W_\$?}$ "
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)\]"
However, we do not hard-code ANSI color escape sequences. Hence we use the tput command as follows:
_GREEN=$(tput setaf 2) _BLUE=$(tput setaf 4) _RED=$(tput setaf 1) _RESET=$(tput sgr0) _BOLD=$(tput bold) export PS1="${_GREEN}\h${_BLUE}@${_RED}\u${_RESET} ${_BOLD}\$ ${_RESET}"
Here is 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 |
Customize bash colors prompt content in Linux or Unix terminal
My current PS1 settings from the ~/.bash_aliases file on Ubuntu Linux desktop displayed using the cat command:
$ cat ~/.bash_aliases
Config that changes the color of shell prompt as per my needs along with the Tux ASCII logo.
export PS1="\[\e[31m\][\[\e[m\]\[\e[38;5;172m\]\u\[\e[m\]@\[\e[38;5;153m\]\h\[\e[m\] \[\e[38;5;214m\]\W\[\e[m\]\[\e[31m\]]\[\e[m\]\\$ " cat<<'EOF' _..._ .' '. / _ _ \ | (o)_(o) | \( ) / //'._.'\ \ // . \ \ || . \ \ |\ : / | \ `) ' (` /_ _)``".____,.'"` (_ ) )'--'( ( '---` `---` EOF
Which will produce:
My customized and colorized bash prompt
Conclusion
You learned how change the color of shell prompt under Linux and Unix when using bash. For more information see the following resources:
- HowTo: Change or setup your bash custom prompt (PS1) – This tutorial explains how-to customizing a bash shell to get a good looking prompt, configure the appearance of the terminal and apply themes using bashish package.
- man page – bash
- How to enable colorized output for ls command in MacOS X Terminal
- How To Turn On/Off Colors For ls Command In Bash On a Linux/Unix
- Turn off color in Linux terminal/bash session
- Turn On or Off Color Syntax Highlighting In vi or vim Editor
- BASH Shell: Change The Color of My Shell Prompt Under Linux or UNIX
🐧 67 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
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)
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\]'
Thank You! Been trying to figure out how to do that for years.
Thanks, still usefull in 2017 ! :D
+ 1
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!
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?
Read the first comment. I had the same issue.
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:
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 :
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.
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
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}"
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.
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.
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?
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.
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).
i want to modify the way dirctory a creaded in bash
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 ;) )
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:
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\] \$ '
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:
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”
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
Can we change the colors of terminal that is appearing on the booting?
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! ;)
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…
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~
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\]'
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\]"
@Jin Thanks a lot! Author should’ve write about that.
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.
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…
These 2 methods work best for me.
These are some good references.
http://www.ibm.com/developerworks/linux/library/l-tip-prompt/
http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
That was helpful, thanks.
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”
Thanks, working fine.
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
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!
Simple with color:
export PS1="\[$(tput setaf 5)\]\h:\[$(tput setaf 6)\]\W\[$(tput sgr0)\]$ "
Thanks for putting this together. This is exactly what I was looking for.
Thanks, really useful :)
Just a note, you have repeated some colors in the table (red, purple, brown).
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)
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).
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 !!
What’s blink code without tput? Would you please example me with echo ?
Nice work
Please edit this command:
export PS1="\e[0;31m[\u@\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.
Kaco was impressed, works 1/1 times, would change color again.