One of the commonly used shell in Linux is bash. Even if there are many modern shells available, it's still the default shell in many Linux distributions.
In this tutorial, we learn how to change the color of specific parts of the bash prompt in Linux. Here we use ANSI escape codes within the PS1 variable.
Introduction to Bash shell prompt
The bash shell prompt is the text displayed on the command line before the user enters a command. By default, the prompt displays the current directory, the user's username, and the hostname of the computer.
PS0, PS1, PS2, PS3, and PS4 are environment variables that determine the appearance and behavior of the bash shell prompt. The value of these variables can be used to customize the prompt, including changing its color.
Edit the environment variables
To print the current value Bash Prompt, type:
echo $PS1
[\[email protected]\h \W]\$
To change the colors of the prompt in bash, you can use the PS1 variable, along with the \[ and \] escape sequences, to specify the colors that you want to use.
Here is an example of how you can colorize your Bash shell prompt:
export PS1="\[\e[31;42m\]\[email protected]\h:\w\$\[\e[0m\]"
In this example, the \e[31:42m sequence sets the text color to red (31) and the background color to green (4
2). The \[\e[0m\] sequence resets the text color to the default value. The \u and \h sequences are replaced with the user name and hostname, respectively, and the \w sequence is replaced with the current working directory.
- \e[ - Start of color modifications.
- Color Code+m - Select a color code.
- \e[0m - End of color change.
You may verify the color using the echo with -e option by interpreting backslash escapes:
echo -e "\e[33mYellow Text Color\e[0m"
Note: The export PS1 allows only a temporary change to your Bash prompt. To make a permanent change you need to edit the ~.bashrc file.
Bash - ANSI Color Escape Codes
The following table lists some of the commonly used bash color codes:
Color | Foreground Code | Background |
---|---|---|
Black | 30 | 40 |
Red | 31 | 41 |
Green | 32 | 42 |
Brown/Yellow | 33 | 43 |
Blue | 34 | 44 |
Purple | 35 | 45 |
Cyan | 36 | 46 |
Light Gray | 37 | 47 |
White | 97 | 107 |
Some additional characters supported by the escape sequence are:
Code | Description |
---|---|
0 | Normal Characters |
1 | Bold Characters |
4 | Underlined Characters |
5 | Blinking Characters |
7 | Reverse video Characters |
Example:
echo -e "\e[1mSample Bold Text\e[0m"
Sample Bold Text
You may also combine colors or characters to make it more elegant. For example
echo -e "\e[1;33;4;47mBrown Underlined Text on Light Gray Background\e[0m"
echo -e "\e[35;32mFancy New Text Color\e[0m"
Customized prompt with Special codes
In addition to using color codes to customize the Linux prompt, special codes can also be used to add information and functionality to the prompt.
By default, the command prompt is set to [\[email protected]\h \W]\$.
Each backslash-escaped special character can be decoded as follows:
\u
: Display the current username.\h
: Display the hostname\W
: Print the base of current working directory.\$
: Display # (indicates root user) if the effective UID is 0, otherwise display a $.
For a Unix user other than root, it will be displayed as below:
[[email protected] ~]$
For example, to add the current date and time to the primary prompt, the value of PS1 can be modified as follows:
export PS1="\[\e[31m\]\d \t\[\e[0m\]\s-\v\$ "
This would display the current date and time in red, followed by the shell and shell version in the default color, and ending with a dollar sign.
Here are some of the escape sequences to customize bash prompt.
\u
Username of the current user.\w
The current working directory.\W
The last fragment of the current working directory. For example, if you are currently in /home/ubuntu/var, this will give you var.\h
The name of the computer, upto a dot(.). For example, if your computer is named node01.example.com, this gives you node01.\H
FQDN hostname.\d
The date in “Weekday Month Date” format (e.g.”Tue 21 March”).\t
The current time in 24 hour HH:MM:SS format.\T
The current time in 12 hour HH:MM:SS format.\@
The current time in 12-hour AM/PM format.\n
Move on to the next line.\!
The history number of this command.\#
The command number of this command.\$
If the effective UID is 0, a #, otherwise a $.\j
The number of jobs currently managed by the shell.
Applying Changes to the Bash shell
To make the bash prompt color or background permanent, you need to update PS1 entries in your .bashrc file. The .bashrc file is a configuration file for the bash shell that is executed every time you start a new bash session.
Open your .bashrc file in a text editor of your choice. This file is usually located in your home directory, and it contains various settings for the Bash shell.
nano ~/.bashrc
Take a backup of .bachrc file before making any changes. In your .bashrc file, find the line that starts with PS1 and contains the current value of the prompt. It should look something like this:
PS1="\[email protected]\h:\w> "
Save the changes to your .bashrc file and close the text editor.
To apply the changes to your current Bash session, you need to source the .bashrc file by running source ~/.bashrc
command.
Finally, you have a permanent bash prompt color that should now be displayed in the terminal.
Using tput command
The tput command is a terminal control tool that allows you to change the appearance of your terminal. It is commonly used in shell scripts to set various terminal attributes, such as text color and style, cursor position, and window size.
One common use of the tput command is to change the text color of your bash shell. To do this, you can use the tput setaf command followed by a number that corresponds to the desired color. For example, to set the text color to red, you can use the following command:
tput setaf 1
or
export PS1="\[$(tput setaf 3)\]\[email protected]\h:\w $ \[$(tput sgr0)\]"
This will set the text color to red until you reset it to the default color using the tput sgr0 command. You can also combine multiple tput commands in a single command line to set multiple attributes at once.
List some of the tput command line options:
tput bold
– Bold effecttput rev
– Display inverse colorstput sgr0
– Reset everythingtput setaf {CODE}
– Set foreground color, see color {CODE} table below for more information.tput setab {CODE}
– Set background color, see color {CODE} table below for more information.
tput color codes
Color | Color Codes |
---|---|
Black | 0 |
Red | 1 |
Green | 2 |
Yellow | 3 |
Blue | 4 |
Magenta | 5 |
Cyan | 6 |
White | 7 |
Conclusion
The visual distinction by color is what we can do in our terminal to make our work more enjoyable and tracking. Colorizing certain information can make them stand out, and can help you locate the last prompt when scrolling through terminal history. You can even customize color patterns to distinguish your root users from others.
Now, it's time for you to utilize this effect to ease your work. I would love to read your valuable comments and suggestions on this.
Comments