Change the Color of Your Linux Prompt

How to Change the Color of your Linux Prompt

Spread the love

 

By default, all Linux installations have plain black and white color prompts. Specific commands are coded to have color combinations under certain circumstances. For example, directories in the “ls” command are coded blue by default, and scripts are colored green.

Similarly, the frequently used “grep” command is also color-coded to highlight matches. However, the Linux prompt itself is rather innocuous. In this article, we’ll show you how to change it to anything you want – a single color, or even a combination of them. Let’s get started.

Getting your Current Prompt Defaults

Every Linux prompt consists of a few key details, allowing the user to keep track of where they are. By default, most have at least the username and the hostname separated by an “@” symbol. This is encoded in a special way using an environment variable called “PS1”. To see what your current variable is, type in the following command:

echo $PS1

This will give you an output similar to this:

[\u@\h \W]\$

Here, “u” stands for the username, and “h” stands for the hostname.

Enclosing Your Prompt in Color Codes

Now, we’re going to enclose this output in color codes with the following opening and closing tags:

\e [colorcode $PS1 \e[m

Here, “colorcode” represents a specific color. These codes are represented by a series of numbers, separated by a semi-colon (;). For example, the following color represents yellow, with bold text:

0;33;1m

The following represents bold text in red:

0;31;1m

The last “1” before the “m” means that the text will be displayed in bold. If you want your text to be purely yellow, it is as follows:

0;33m

To make something bold, we add a “1” separated from the rest by a semi-colon. As you can see, different numbers represent different colors. Here’s a small list of numbers, along with the colors they represent:

30 Black 

31 Red 

32 Green 

33 Yellow 

34 Blue 

35 Magenta 

36 Cyan 

37 Light gray 

90 Dark gray 

91 Light red 

92 Light green 

93 Light yellow 

94 Light blue 

95 Light magenta 

96 Light cyan 

97 White

Creating a New PS1 Variable

Now that we know how to create colors, we can enclose the PS1 variable within the opening and closing tags of the colors. To re-iterate, the format is:

\e [colorcode[PS1 Variable] \e[m

Since our PS1 variable is:

[\u@\h \W]\$

And the color code for bold yellow is:

0;33;1m

The final value for a bold yellow color prompt is:

\e[0;33;1m[\u@\h \W]\$ \e[m

And we assign it to the PS1 variable like this:

export PS1="\e[0;33;1m[\u@\h \W]\$ \e[m"

When we run this command, we get the following output as expected:

Change Prompt to Yellow Bold Text
Changing the Prompt to Yellow Bold Text

If we want, we can even have the username and hostname in different colors. To do this, we enclose the “u” and the “h” in different color codes.

For example, the following code converts the username to bold yellow and the hostname to bold red:

export PS1="\e[0;33;1m[\u\e[m@\e[0;31;1m\h\e[m \W]\$"

Making the Changes Persistent

The disadvantage of changing everything ad-hoc like this is that your changes will be lost when you reload the bash environment. To make our terminal remember our configuration, we need to add the command to our .bashrc file, found in our home directory. Open it with your preferred text editor:

nano ~/.bashrc

Simply open it in the editor, and add your export command to the end, like this:

Modify .bashrc
Modify .bashrc to Make Changes Persistent

Save your changes. Now when you log in, you should have your customized prompt for your commands!

New Color Scheme Persists After Rebooting
The New Color Scheme Persists Even After Rebooting

To have the changes take effect immediately instead of having to log out and log in, run the following command to reload the .bashrc file:

source ~/.bashrc

While this might not be the most user-friendly user interface for changing your prompt colors, it is the most common way to change the colors. If you followed the guide correctly, you should now have a command prompt in the color of your choosing. 


Of course, you don’t have to do any of this if you use one of our Outsourced Server Support Services, in which case you can simply ask our expert Linux admins to configure the appearance of your command prompt for you. Just sit back, relax, and let our admins take care of the issue for you. They are available 24×7 to help you with your requests.

PS. If you liked this post on changing the color of your linux command prompt, please share it with your friends on the social networks by using the share shortcut buttons, or simply leave a comment in the comments section below. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *