Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: How to customize and change color of the bash login prompt in Linux  (Read 1397 times)

0 Members and 1 Guest are viewing this topic.

akhilt

  • Guest
How to customize and change colour of the bash login prompt in Linux

Here, I will explain two methods that we can use to customize and change the colour of the login prompt of our bash shell in Linux.

1. Octal Method
2. Bash Method

Before that, let's take a look at the different colour code that we are going to use here:
Code: [Select]
Black           ->  30
Red             ->  31
Green           ->  32
Yellow          ->  33
Blue            ->  34
Magenta         ->  35
Cyan            ->  36
White           ->  37
Bright Black    ->  90
Bright Red      ->  91
Bright Green    ->  92
Bright Yellow   ->  93
Bright Blue     ->  94
Bright Magenta  ->  95
Bright Cyan     ->  96
Bright White    ->  97

The following additional prompt must be added additionally to each method:
Code: [Select]
start   -> \[
end     -> \]

1. Using Octal Format

The starting and ending of the command should be like this:
Code: [Select]
start   -> \033[0m
end     -> \033[m

Now, to Change the color of the prompt to 'RED':
Code: [Select]
# export PS1='\[\033[31m\][\u@\h ~]# \[\033[m\]'

For Example, Here is my login prompt:


Now, after changing the colour to RED:


Below image shows the complete list of color codes:


Similarly, you can modify the command to change the prompt to different colors, you only need to care about below format while using the above command:
Code: [Select]
start   -> \[\033[31m\]  <-- Here modify '31m' with the color number
end     -> \[\033[m\]    <-- to reset the terminal or else your commands would also appear with the same color

2. Using Bash Format

The starting and ending of the command should be like this:
Code: [Select]
start   -> \e
end     -> \e[m

Now, to Change the color of the prompt to 'RED':
Code: [Select]
export PS1='\[\e[31m\][\u@\h]#\[\e[m\] '

For Example, Here is my login prompt:


Now, after changing the colour to RED:


Similarly, you can modify the command to change the prompt to different colors, you only need to care about below format while using the above command:
Code: [Select]
start   -> \[\e[33m\]  <-- Here modify '31m' with the color number
end     -> \[\e[m\]    <-- to reset the terminal or else your commands would also appear with the same color

I hope the article was interesting :) ;D!!