How to Customize Bash Colors and Content in Linux Terminal Prompt - GeeksforGeeks

14 Apr, 2022
If you are using the Linux operating system, that means you use the CLI most of the time. And do more work on the terminal. By default, most Linux Operating systems provide you the bash shell. Shell provides the interface between the user and kernel and executes commands. In this article, we are going to see how to customize the bash shell prompt
Before customizing the bash shell prompt first understand the default bash prompt. This default prompt looks like follows:
username@hostname:~$
or 
[username@hostaname ~]$
The first part i.e string before @ character of bash prompt indicates the username of the current user. The last part of the bash prompt indicates the hostname of the system. Then the ~ sign indicates the current path of the prompt. If the sign after the : or ] character is $ that means the account is standard or if this character is # then the account is the root
To customize the bash prompt, first, we should understand how the bash prompt works. Bash provides the Prompt Statement. There are four bash prompt statement
PS1 – This is the primary prompt statement. We will customize this prompt.
PS2 – This is the secondary prompt statement. Basically, it is used when the user provides the long command separated by \ characters.
PS3 – This prompt is used to select the command.
PS4 – This prompt is used for running a shell script in debug mode.
To see the value of your current PS1 prompt statement, you can use the following command:
echo $PS1
The ps1 has contained the backslash and other alphabetic characters which has a special meaning which is listed in the PROMPTING section of the man page. In the above output, we can see that \u \h and \W are the prompting characters and @ and # are special characters.
To customize the bash prompt, we are going to work on the PS1 prompt and PS2 prompt. Generally, the PS2 prompt contains only one character >.  To view the content of the PS2 prompt use the echo command:
echo $PS2
And the $PS3 will be blank and PS4 will contain the + character
The bash prompt can be customized from the ~/.bashrc file. This file contains the prompt Statement. This file is present in the home directory of the user.
~/.bashrc
Before editing this file, make the backup of the ~/.bashrc file. Use the following command to make a backup of the ~/.bashrc file
cp ~/.bashrc  ~/.bashrc.bak
To change the bash prompt permanently, we can edit the file ~/.bashrc and change the values of the PS1. To edit this file you can use any editor, but in this tutorial, we are going with nano editor, because it is easy to use. Now to open the ~/.bashrc file, use the following command:
nano ~/.bashrc
Then you will see there is a PS1 variable.
You can edit the value of this variable to change your prompt. For now, let’s change the value of this prompt to bashprompt>. Then save the file using the ctrl+s and then close the file using ctrl+x. Then use the following command to see changes in prompt
source ~/.bashrc
Now we have changes our bash prompt permanently.
We can change our bash prompt temporarily using the export command, this prompt will work for the current session. To change the temporary bash shell prompt, use the following command:
export PS1="bashprompt>"
Or you can just run to enter the PS1 variable with value  as a command:
Most of the Linux distributions contain the username@hostname as a bash prompt. We can change it to anything we want. We have to just modify the value of the PS1 variable. In the above two sections, we have seen how to modify the value of the PS1 characters permanently and temporarily. Change the value of PS1 according to your need. So now  to change the username@hostname to “myprompt@linux> ” we can set the value of PS1 to 
export PS1="myprompt@linux> "
Now let’s see how to add emojis in the bash prompt. To add the emojis to the prompt, first, make sure that you have installed any emoji font on the system. To use the emoji in the prompt, just put the emoji in the PS1 variable. Here is one example:
 PS1="???? ~ "
To show the version of bash shell in the prompt, put the \v prompting character in the PS1 variable:
PS1="Bash \v>"
And to show the current bash version with the patch level, use the \V prompting character:
 PS1="Bash \V>"
To customize the PS1 prompt, we need to edit the content of the PS1 prompt. The PS1 contains some characters followed by the backslash characters. Following are the same characters that are written in the PS1 prompt:
\u: This character indicates the username of the current user.
\h: This character indicates the hostname till the first ‘ . ‘ Character in the Fully-Qualified Domain Name
\W: This character shows the base path of the current working directory. For the home directory, the value will be tilde (~) character.
\$: This character is used to separate the command and prompt. If the account is standard then this field contains $ character, or if the account is root then this field contains the # character.
Now let’s add some other options in PS1 and check how our prompt looks like the \! Character shows the number of the current commands and \H character shows the Full  Fully-Qualified Domain hostname instead of showing till ‘ . ‘ Character. Here is the prompt now:
PS1="[\u@\H \W \!]$"
In the next sections, we are going to explore more prompting options or characters.
Now let’s see how can we customize the bash prompt using the options provided by the bash shell for the prompt. Before adding any option to the prompt, use the \ character before the options.
Bash prompt provides two options, by using these we can show hostname and username in prompt. 
To show username in prompt, use u character followed by \ character.
To show hostname in prompt, use h character in PS1.
Here is one example:
export PS1="\u \h >"
We can add the special character in the bash prompt. Just arrange them in order how you want to customize the prompt. Here is one example:
export PS1="\u@\h> "
You should always use the special character at the end of the prompt, which will be useful to separate the command and prompt.
Now let’s see how we can add the time to the bash prompt. Following are the options which will be used to display date and time in prompt
d – This option will show the date in “Weekday Month Date”  format
              
t – This option will show the current time in 24-hour HH:MM:SS format
T – This option will show the current time in 12-hour HH:MM:SS format
A  –  This option will show   the current time in 24-hour HH:MM format
To prevent the showing username and hostname into prompt just don’t use the h and u characters in the PS1 variable. Just use the W character to display the path of the current directory.
The bash prompt is differentiated using the $ and # characters at the end of the prompt. The $ character is used for the standard user and the # character is used for the root user.
export PS1="\u@\H \W:\$ "
To know all color options, you read the PROMPTING section of the man page of bash. Using the man command.
tput is a command that provides the terminal dependent information to the shell .tput command queries the term info database for the information. Now let’s see how we can use the tput command to change the prompt color. Now let’s see how to change the color of the background and foreground of the prompt.
export PS1=”\[$(tput setaf 1)\]\[$(tput setab 7)\]\u@\h:\w $ \[$(tput sgr0)\]”
Following are the options that can be used with the tput command:
tput bold –To apply the bold effect
tput rev – To display inverse color
tput sgr0 – To reset everything
tput setaf {code}– To set the foreground color. See the table below to know the value of {code}
tput setab {code}– To set background color, See the table below to know value of {code}
Color codes that are used with tput command:
We can change the color of the bash prompt. Here is one example:
export PS1="\e[0;32m[\u@\h \W]\$ \e[0m"
Now let’s see how we can change the color of the bash prompt:
\e[  – This string tells bash prompt to apply color from next character.
0;32m  – This string represents the colors. The number before the; represent typeface. And the number after the ; represent color code.
\e[0m – This string will tell the bash prompt to apply the color to the previous character.
Following are the values for the typeface:
0 – Normal
1 – Bold
2 – Dim
4 – Underlined
Following are the values for the color codes:
30 – Black
31 – Red
32 – Green
33 – Brown
34 – Blue
35 – Purple
36 – Cyan
37 – Light gray
You can create the themes using different combinations of the above colors.
If you want the shell back as it is, then we can do that. At the start of this article, we have created the backup file the ~/.bashrc file. Now to get back our original bash prompt, we can use that file. Use the following command:
cat ~/.bashrc.bak > ~/.bashrc
To know more about the bash prompt, read the man page of the bash.
man bash
shivamshubham
Picked
How To
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install FFmpeg on Windows?
How to Add External JAR File to an IntelliJ IDEA Project?
How to Set Git Username and Password in GitBash?
How to Install Jupyter Notebook on MacOS?
How to Create and Setup Spring Boot Project in Eclipse IDE?
Sed Command in Linux/Unix with examples
AWK command in Unix/Linux with examples
grep command in Unix/Linux
cut command in Linux with examples
cp command in Linux with examples