Bash (Bourne Again Shell) is a superset of the Shell Command Language (SH) that allows for a lighter, more legible syntax that brings improvements. At baseline, a bash command or a bash script receives input data from a terminal, from a file descriptor or from a string, then runs and outputs output data.
Linux distributions often use a classic prompt in the terminal, displaying the user name, hostname, and current directory. After a while, you’re kind of getting bored to see the same thing every time, so you will start thinking of changing and customizing the Linux prompt.
The prompt is controlled by a special shell variable PS1.
To display the current value of the variable, enter the command:
At the exit you will have something like:
[\[email protected]\h \W]\$
This means that the username (\u), the @ character, the hostname (\h), the current directory (\W), and the character $ (or # for root) are displayed.
But I want something else to be displayed. For this, we need to assign another value to the $PS1 variable; write the command below with the new desired character string in the simple quotes:
export PS1=’this is my new prompt:’
Press ENTER and the new prompt will appear in the terminal. But the value is maintained only during the session so the next initialization of the terminal will have the old prompt.
Permanent change of the prompt
To permanently change the value of the prompt, we need to edit the .bashrc file in the home directory of each user.
We’ll comment on the line that defines the current prompt, putting the character # before it (maybe we’d like to come back to it sometime). After saving with CTRL + X, if we reopen the terminal, the new set value will appear.
Changing the color of the prompt
If we do not tell it anything, the new prompt will be displayed with the terminal theme color. But we can also change the colors of the prompt Linux. By default, the Linux terminal uses 8 colors; enter the command tput colors to convince yourself about this. To change from 8 colors to 256 colors, we will modify another variable in the same .bash.rc file (also shown in the example above):
Now, the tput colors command will have a different answer.
Below are the color codes (works on most terminals):
Black 0; 30
Blue 0; 34
Green 0; 32
Cyan 0; 36
Red 0; 31
Purple 0; 35
Brown 0; 33
Light Gray 0; 37
Dark Gray 1; 30
Light Blue 1; 34
Light Green 1; 32
Light Cyan 1;36
Light Red 1; 31
Light Purple 1; 35
Yellow 1, 33
White 1; 37
Value 1 shows the lighter color variation.
So, to add Linux prompt colors you have to enter this command:
‘\e[x;ym $PS1 \e[m’
• \e[ – starts a color scheme
• x;y – the pair of numbers corresponding to a color (x; y)
• $PS1 – the prompt variable
• \e[m – completes the color scheme
You can play and display each word of the Linux prompt in different colors.