03/02/2018 - LINUX, MAC
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.
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\]\$
u
- Represents the "user" ubuntu
.@
- Represents the @
sign.h
- Represents the "host" linux
.:
- Represents the :
sign.w
- Represents the "working directory" ~
.$
- Represents either $
or #
sign. #
is displayed for user whose "UID" is "0" (often "root") otherwise $
is displayed for any other user.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.
It is done with 1;32m
which represents colour pair (x;y).
x
- Represents text weight. 0
or no number represents light, 1
represents bold text.y
- Represents the colour.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
\[\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\]\$
After changing colours, you will need to either exit terminal or open a new terminal to see the changes.
~/.bashrc
file.#force_color_prompt=yes
line and uncomment it by removing #
sign.if [ "$color_prompt" = yes ]; then
, change the colour string and save the changes.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\]\$ '
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