While ago when I was watching someone’s youtube and found that his terminal command line prompt is colored and I found it actually very useful cause sometimes when too many terminals are opened, it was actually very easy to get confused! Therefore if colors could be added to command line prompt, it would be very helpful to distinguish between each terminal connections and somehow prevent you from entering commands to the wrong server.
Let’s first take a look of the difference between before and after.
Before we start, let’s take a look at my hostname first. I set it to a FQDN purposely so we can see the differences later on.
For more information about hostname usage, please visit :
Set Or Change Hostname In CentOS7
Command line prompt setting is store in a variable called PS1, we can check the current(default) setting using
1 echo $PS1
We can break the output into four parts which are :
\u; prints out the current login username, which is root.
\h; prints out the hostname, which is www, as shown in hostname status. If we want to show the FQDN, we can change it to
\W; prints out the current working directory, which is ~, the home directory of login user. If we want to show the full working directory, we can change it to
$when user is login,
#when root is login.
$PS1 variable can be saved in
/etc/bashrc which will affect all users or
/home/username/.bashrc which will only affect that particular user.
Now we understand the basic components of the command line prompt, let’s put up the seven components of the “after” result, which is
\[email protected]\H:\w\$ , and save it as
PS1='\[email protected]\H:\w\$ ' at the bottom of
/etc/bashrc. Please note that there is a space after the
$ sign, so there will be a space between the
# sign and the cursor. The seven components are :
- A space
Re-login to check the result.
Now let’s start adding colors to the prompt. The basic syntax of a color is
\[\e[xx;yy;zzm\] where :
\[\e[m\]is the structure of the color syntax.
xx;yy;zzrepresents the text formate, text color and text background color. xx, yy and zz are unique numbers so you can put them in any order you wish.
A detailed color table is below :
|Text Fromat||Text Color||Text Background Color|
|0: normal text||30: Black||40: Black|
|1: bold||31: Red||41: Red|
|4: Underlined text||32: Green||42: Green|
|33: Yellow||43: Yellow|
|34: Blue||44: Blue|
|35: Purple||45: Purple|
|36: Cyan||46: Cyan|
|37: White||47: White|
With all the infomatin we got, we can break the “after” result into :
\[\033[0;37m\]( please note that there is a space after
So the final result will be as follow :
Put it at the bottom of
/etc/bashrc, save and exit. Re-login to see your terminal with pretty colorful prompt!!