By default, many Linux installations come with simple black-and-white color prompts. However, because a Linux user spends a lot of time in the terminal emulator, it would be nice if we could visually improve our experience.
So, in keeping with the “A picture is worth a thousand words” maxim, let us show what we mean.
We want this:
To become this:
One of the best things about Linux is the unlimited options for customization. The command prompt is no exception. But before we proceed further, let’s get into some theory.
Understanding PS1 (Prompt Statement) in Linux
Your current Bash prompt setting is saved in a shell variable named PS1, which stands for Prompt Statement. In Linux, PS1 is known as a primary prompt variable. This variable is defined and saved in the $HOME/.bashrc
file.
Furthermore, to be precise, there are three additional variables, including PS2, PS3, and PS4. However, because their purpose is unrelated to changing the colors of our prompt in the Linux terminal, we will not go into depth about them.
To see the current PS1 value, type the following:
echo $PS1
Code language: PHP (php)
The terminal will output something like this for the default settings depending on the Linux distribution used. In our example, this is the result of a Linux machine running Ubuntu 22.04.
I know; it seems confusing. But don’t worry; the lines below will make everything more transparent.
For the time being, ignore everything in the line above and concentrate solely on the \u@\h:\w\$
part. That is what we are concerned about. Here, u
stands for the username, h
stands for the hostname, and w
stands for the current working directory.
And to make things even more precise, let’s put it this way:
I hope this has clarified things for you. So, let’s move on to the meat of our guide.
How to Change the Colors of Your Bash Shell Prompt on Linux
First, open the .bashrc
file in your favorite text editor and see if the line force_color_prompt=yes
is present. Then, remove the comment (the #
symbol) before it and save the file if the line exists.
vim ~/.bashrc
And now comes the time to get our hands dirty with the actual work of making our Linux shell prompt colorful. So, open the .bashrc
file and look for the line that defines our PS1
variable.
vim ~/.bashrc
I know that may appear incomprehensible to you, but don’t let that bother you. Just remember that this is the variable that determines the appearance of your command prompt in Linux.
The following step is to define our new PS1
variable. But, to be safe, let’s comment it out so we can always refer back to it if we mess up.
Then we will create a new PS1
variable by adding the following line, which colors your Linux terminal prompt.
PS1='\[\033[1;32m\]\u\[\033[1;37m\]@\[\033[1;36m\]\h\[\033[00m\]:\[\033[1;34m\]\w\[\033[00m\]\$ '
Code language: JavaScript (javascript)
Don’t worry if you don’t understand what it means; it’s explained below.
To customize your Bash prompt, you just have to add, remove, or rearrange the special characters in the PS1
variable. Pay close attention to the parts, for example, 1;32m
, 1;37m
, 1;36m
, etc. These are Bash’s color codes. These codes are represented by a series of numbers, separated by a semi-colon plus added m
letter.
Here is a list of the color codes available at Bash Shell.
As you have already guessed, it is simply a matter of replacing/adding values. For example, the value preceding u
refers to the username part. The value preceding the @
character is for the @
character. The value preceding h
corresponds to the hostname part and so on.
When you’re finished, save your modifications and exit the .bashrc
file. To put the changes immediately into effect, execute:
source ~/.bashrc
And that’s it. Our Linux system now has a color Bash shell prompt.
Conclusion
You should now be able to change the colors of your Bash shell prompt on Linux. Then, of course, you can pick the colors you like most and design the color scheme that suits you best.
I highly recommend these two guides for a more in-depth look at the possibilities for customizing the shell prompt in Linux: here and here.
I hope you found the article interesting. Please express your thoughts in the comments section below. Your feedback and comments are much appreciated.