Is there an easy way to change my Bash prompt to modify the colors and what text is displayed? I've heard that this is done in .bashrc
, but I haven't found any nice, easy ways of modifying it. How are colors expressed in Bash?

- 829,060

- 39,676
4 Answers
I once used to have those defined in my .bashrc
:
export e="\e["
function cls { echo -n "${e}H${e}J${e}0m"; }
function rcls { echo -n "${e}H${e}J${e}S${e}H${e}J${e}0m${e}u${e}J"; } # not quite yet !
# rcls only works when no funny codes have been issued in between, i.e. the buffer is clean
# below the current screen. And then there can be issues with the first line of the buffer.
function bright { echo -n "${e}1m"; }
function normal { echo -n "${e}0m"; }
function colour { echo -n "${e}$1;$2;$3m"; }
function black { echo -n "${e}30m"; }
function bg_black { echo -n "${e}40m"; }
function red { echo -n "${e}31m"; }
function bg_red { echo -n "${e}41m"; }
function green { echo -n "${e}32m"; }
function bg_green { echo -n "${e}42m"; }
function yellow { echo -n "${e}33m"; }
function bg_yellow { echo -n "${e}43m"; }
function blue { echo -n "${e}34m"; }
function bg_blue { echo -n "${e}44m"; }
function magenta { echo -n "${e}35m"; }
function bg_magenta { echo -n "${e}45m"; }
function cyan { echo -n "${e}36m"; }
function bg_cyan { echo -n "${e}46m"; }
function white { echo -n "${e}37m"; }
function bg_white { echo -n "${e}47m"; }
function c_up { echo -n "${e}$1A"; }
function c_down { echo -n "${e}$1B"; } # within
function c_right { echo -n "${e}$1C"; }
function c_left { echo -n "${e}$1D"; } # screen
function c_pos { echo -n "${e}$1;$2f"; } # [Hf]
function c_line { echo -n "${e}$1d"; }
function screentop { echo -n "${e}H"; } # [Hdf]
function linetop { echo -n "${e}G"; }
function buffertop { echo -n "${e}u"; } # depends on other control characters ?
function tab { echo -n "${e}I"; }
function backtab { echo -n "${e}Z"; }
function scrolldown { echo -n "${e}$1T"; } # within screen
function scrolldown_B { echo -n "${e}$1L"; } # scroll down below cursor
function scrollup_B { echo -n "${e}$1M"; } # scroll up below cursor
function clear { echo -n "${e}J"; } # delete to end of screen
function cleanbuffer { echo -n "${e}S"; } # copies first screen to bottom and clears
# everything else above the cursor.
#function xxx { echo -n "${e}xxx"; }
export -f bright normal colour
export -f black bg_black red bg_red green bg_green yellow bg_yellow
export -f blue bg_blue magenta bg_magenta cyan bg_cyan white bg_white
export -f c_up c_down c_right c_left c_pos c_line
export -f screentop linetop buffertop tab backtab
export -f scrolldown scrolldown_B scrollup_B clear cleanbuffer
You can then use them for example with:
PS1_init="\n$(bright)$(black)$(hostname):\w\n$(bg_blue)"
PS1_end="$(normal)$(bright)\n\! -> $(normal)$(clear)"
export PS1="$PS1_init"'$(date)'"$PS1_end"
These may help you.
The reason I made them into functions rather than variables is laziness. I just wanted to cut out on typing. Of course if you look for efficiency variables would be better.
At the end of the day though these only fit certain terminals. So for any help consult your terminal documentation, not that of bash or any other shell for that matter.

- 2,495
-
-
1Variables would save on typing:
bright=$'\e1m'
and so on. (Unless you need compatibility with very old versions that didn't have the$'…'
syntax.) – Gilles 'SO- stop being evil' Apr 30 '11 at 12:17 -
@Gilles, you misunderstood: I prefer to type
blue
at the prompt (or in a shell script) thanecho $blue
. – asoundmove Apr 30 '11 at 14:48
Bash uses the Ansi color scheme - Wikipedia article: http://en.wikipedia.org/wiki/ANSI_escape_code#Colors
For example, here is my prompt: (I like my prompt on a separate line, but not everyone does. Also, my terms have dark background - adjust colors if your terms are light.)
export PS1='\[\e[0;36m\]\u\[\e[0m\]@\[\e[31m\]\h \[\e[0;36m\]\t \[\e[93m\]\w\[\e[36m\] p$$-\!-\$\[\e[0m\]\n\$ '
The special prompt values are described in the Bash manpage under "Prompting":
\u the username of the current user
\h the hostname up to the first `.'
\t the current time in 24-hour HH:MM:SS format
\w the current working directory, with
$HOME abbreviated with a tilde
(uses the value of the PROMPT_DIRTRIM variable)
$$ PID of the Bash (helps distinguish my many terms.
Useful, eg, if I have to kill something.
\! the history number of this command
\n newline
\$ if the effective UID is 0, a #, otherwise a $

- 129
- 3
-
1Bash does not use the ansi colour scheme. It is the terminal that implements the escape codes. Bash will work on any terminal, so it may be best to use
tput setf _n_
to get the escape code to set the foreground colour. – camh Apr 29 '11 at 23:24
for example for coloring you can do this:
Simply add the following line:
export PS1=" \[\033[34m\]\u@\h \[\033[33m\]\w\[\033[31m\]\[\033[00m\] $ "
This is my preferred colors. You can customize each part of prompt's color by changing m
codes (e.g. 34m
) which are ANSI color codes.
List of ANSI Color codes:
- Black: 30m
- Red: 31m
- Green: 32m
- Yellow: 33m
- Blue: 34m
- Purple: 35m
- Cyan: 36m
- White: 37m

- 101
None of these answers helped me learn how prompt customization works. Here's what I gathered from a few hours' searching around various different forums, Stacks, wikis, etc:
Find your prompt
nano ~/.bashrc
Search your bashrc
for PS1
, either manually or with Alt+F. This is the first bash prompt, what you see when you get to enter a command. PS[2-4]
also exist, but those are rarely used.
Define colors
Above you're PS1
line, add these color definitions:
# Color Variables
c1='\[\033[0;30m\]' # Non-bold color 1
C1='\[\033[1;30m\]' # Bold color 1
c2='\[\033[0;31m\]' # Non-bold color 2
C2='\[\033[1;31m\]' # Bold color 2
c3='\[\033[0;32m\]' # Non-bold color 3
C3='\[\033[1;32m\]' # Bold color 3
c4='\[\033[0;33m\]' # Non-bold color 4
C4='\[\033[1;33m\]' # Bold color 4
c5='\[\033[0;34m\]' # Non-bold color 5
C5='\[\033[1;34m\]' # Bold color 5
c6='\[\033[0;35m\]' # Non-bold color 6
C6='\[\033[1;35m\]' # Bold color 6
c7='\[\033[0;36m\]' # Non-bold color 7
C7='\[\033[1;36m\]' # Bold color 7
c8='\[\033[0;37m\]' # Non-bold color 8
C8='\[\033[1;37m\]' # Bold color 8
NC='\[\033[0m\]' # Back to default color
Configure colors
Now somehwere in your (presumably graphical) terminal you should have some customization option. For example, I'm using KDE's Konsole: Settings > Edit Current Profile... > Appearance > Edit...
shows this customization interface:
Now change these to whatever colors you like. Your foreground is your default color, and Colors 1~8 are the ones you defined that you can choose. Not that the definitions are 0-based while this customization and your variables are 1-based.
Use the colors in your prompt
For my default prompt, I have:
PS1="[\u@\h \W]\$ "
\u
is your username, \h
is your hostname, \W
is Where you are (which directory), and \$
is the prompt thingy (#
when userid=0 [root]).
You can customize this by adding $c1
, $c2`, etc. in front of the color you want to change. For example, I have:
PS1="$c2[$C8\u$c2@$C7\h $C6\W$c2]$C2\$$NC "
This results in:
Note: You need to use NC
at the end to reset it to your default color, or else everything after your prompt (your input) will be whatever the last color in your prompt is.

- 393
bash+prompt
, though I don't think we have a tutorial in there. – Gilles 'SO- stop being evil' Apr 29 '11 at 21:16