In this example we are going to change terminal prompt colour. Having different colours for different users and servers can be useful. Having the same colour for all users and servers might sometimes cause people doing wrong things with wrong users and on wrong servers. Colours will tell us who we are and where we are.


Explanation


Structure


Colouring terminal prompt ubuntu@linux:~$ is done with a special string which looks like the one below.


\[\033[1;32m\]\u\[\033[00m\]@\[\033[1;32m\]\h\[\033[00m\]:\[\033[1;32m\]\w\[\033[00m\]\$


The \[\033[1;32m\]\ part is used for colouring and placed in front of u, h, w and $. The last \ is not used for @ and : signs.


Colouring


It is done with 1;32m which represents colour pair (x;y).



Black       0;30     Dark Gray     1;30
Blue 0;34 Light Blue 1;34
Green 0;32 Light Green 1;32
Cyan 0;36 Light Cyan 1;36
Red 0;31 Light Red 1;31
Purple 0;35 Light Purple 1;35
Brown 0;33 Yellow 1;33
Light Gray 0;37 White 1;37

Example


\[\033[1;32m\]\u\[\033[00m\]@\[\033[1;32m\]\h\[\033[00m\]:\[\033[1;32m\]\w\[\033[00m\]\$

\[\033[1;35m\]\u\[\033[00m\]@\[\033[1;36m\]\h\[\033[00m\]:\[\033[1;31m\]\w\[\033[00m\]\$

\[\033[31m\]\u\[\033[34m\]@\[\033[33m\]\h\[\033[35m\]:\[\033[36m\]\w\[\033[00m\]\$


Changing colour profile


After changing colours, you will need to either exit terminal or open a new terminal to see the changes.


Linux


  1. Open up ~/.bashrc file.


  2. Find #force_color_prompt=yes line and uncomment it by removing # sign.

  3. Find if [ "$color_prompt" = yes ]; then, change the colour string and save the changes.

Mac


Create a .bash_profile file with the content below.


inanzzz@macos:~$ nano ~/.bash_profile

export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced
export PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

References


Create and run bash script below to list all available colours and their codes.


!/bin/bash

color=16;

while [ $color -lt 245 ]; do
echo -e "$color: \033[38;5;${color}mhello\033[48;5;${color}mworld\033[0m"
((color++));
done