UNIX & GNU/Linux - bash - Setting the colour prompt with PS1

You like your shell but you think that some colours would be better.

Let's see how to set colours of the bash prompt with the PS1 variable.
Notice that this tutorial has been made with Ubuntu 12.
Some syntaxes may change depending on the operating system.

First of all, at your home directory, that you can have with the command "~/", there is a configuration file named: .bashrc.

If you don't see it, type this:

ls -la

Then open it with an editor, for example gedit:

gedit .bashrc

Inside the file, find the line:

#force_color_prompt=yes

Uncomment it by removing the # character.

force_color_prompt=yes

Now we have to find the following code:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

What is interesting for us is the PS1 variable.
This is what we are going to modify.

The syntax is not so easy to understand and even weird, but we can do it.

The code has to be like this:

\[\e[FIRST;SECONDm\]

The FIRST parameter to modify the behaviour

\[\e[FIRST;SECONDm\]

The FIRST number is for specified the type of the display.
The following code transform the text:

  • 0 = regular
  • 1 = bold
  • 2 = darker regular
  • 4 = underline and bold
  • 7 = set the background colour
  • 8 = invisible
  • 9 = stroke

The value 3, 5 and 6 are not used.

To illustrate it, I give you an example.
For that we set the content of the PS1 variable.
I used "\" at the end of each line to tell the shell that the command is not finished and that the further is on the next line.
I also set the colour to green (32) with the SECOND parameter.
And I reset the behaviour with \[\e[m\] after each command.

PS1='\[\e[0;32m\]0-Regular\
\[\e[m\] \
\[\e[1;32m\]1-Bold\
\[\e[m\] \
\[\e[2;32m\]2-Darker regular\
\[\e[m\] \
\[\e[4;32m\]4-Underline\
\[\e[m\] \
\[\e[7;32m\]7-Set background\
\[\e[m\] \
\[\e[8;32m\]8-Invisible\
\[\e[m\] \
\[\e[9;32m\]9-Strike\
\[\e[m\]'

Save the .bashrc file and on your shell, put your prompt to the directory where there is this file then type this:

. .bashrc

Your prompt looks now like something like that with of course correct behaviour:

0-Regular 1-Bold 2-Darker regular 4-Underline 7-Set background             9-Strike

The second parameter (SECOND) is for set the colour.

The SECOND parameter for setting the code colour

We are going to set the SECOND paramater:

\[\e[FIRST;SECONDm\]

The code colour is as following:

  • 30 and 90 = black
  • 31 and 91 = red
  • 32 and 92 = green
  • 33 and 93 = yellow
  • 34 and 94 = blue
  • 35 and 95 = purple
  • 36 and 96 = cyan
  • 37 and 97 = white

The first number is for a darker colour. The second for a lighter one.

The example took previously for the FIRST parameter will help us to this new example.
I use this time some codes instead of only green (32).

PS1='\[\e[0;31m\]0-Regular\
\[\e[m\] \
\[\e[1;32m\]1-Bold\
\[\e[m\] \
\[\e[2;33m\]2-Darker regular\
\[\e[m\] \
\[\e[4;34m\]4-Underline\
\[\e[m\] \
\[\e[7;35m\]7-Set background\
\[\e[m\] \
\[\e[8;36m\]8-Invisible\
\[\e[m\] \
\[\e[9;37m\]9-Strike\
\[\e[m\]'

The result is like the first but with more colours.

Or for a new full prompt with a french flag:

PS1='\e[7;34m \e[7;37m \e[7;31m \e[0m \e[01;33m\D{%m/%d}\e[01;00m-\e[01;34m\D{%H:%M} \e[01;32m\w\e[0m
> '​
 

You can now color your prompt like a professional.
Good job, you made it! laugh

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.