Home BASH How To Customize Bash Prompt In Linux

How To Customize Bash Prompt In Linux

Personalize Your BASH Prompt in Linux: Add Colors and Styles the Manual Way

By sk
Published: Updated: 19.3K views

BASH (the Bourne-Again Shell) is the default shell for most modern Linux distributions. Tired of the same old boring BASH prompt? Let's give it a makeover! In this guide, we are going to learn how to customize the BASH prompt and enhance the bash prompt's look by adding some colors and styles.

Of course, there are many plugins/tools available to get this job done easily and quickly. But in this guide, we'll keep it simple. We won't install any extra stuff.

We are going to do some basic customization, such as adding, modifying elements, changing the foreground and background color etc., without any additional tools or plugins.

By the end, your terminal will look exactly how you want it to. Let's start customizing your BASH prompt together!

Customize Bash Prompt in Linux

In BASH, you can customize and change the BASH prompt the way you want it by changing the value of PS1 environment variable.

Usually, the BASH prompt will look something like below:

BASH prompt in Ubuntu
BASH prompt in Ubuntu

Here, sk is username and ubuntuserver is hostname.

Now, we are going to modify this Bash prompt by inserting some backslash-escaped special characters called Escape Sequences.

Let me show you some examples.

It is always a good practice to backup the ~/.bashrc file before making any changes in it.

$ cp ~/.bashrc ~/.bashrc.bak

Modify "username@hostname" part in the Bash prompt:

As I mentioned above, the BASH prompt has "username@hostname" part by default in most Linux distributions. You can change this part to something else.

To do so, edit ~/.bashrc  file:

$ vi ~/.bashrc

Add the following line at the end:

PS1="ostechnix> "

Replace "ostechnix" with any letters/words of your choice. Once added, hit the ESC key and type :wq to save and exit the file.

Run the following command to update the changes:

$ source ~/.bashrc

Now, the BASH prompt will have the letters "ostechnix" in the shell prompt.

Customize bash prompt in linux
Modify "username@hostname" part in the Bash prompt

Here is another example. I am going to replace "username@hostname" part with "Hello@welcome>".

To do so, add the following entry in your ~./bashrc file.

export PS1="Hello@welcome> "

Update the changes by running this command:

$ source ~./bashrc

Here is how my my BASH prompt in Ubuntu system after changing the default values.

Modify BASH prompt in Ubuntu
Modify BASH prompt in Ubuntu Linux

Display username only:

To display the username only, just add the following line in ~/.bashrc file.

export PS1="\u "

Here, \u is the escape sequence.

The following are some other example values that you can add to your PS1 variable to change the BASH prompt. After adding each entry, you must run source ~/.bashrc command to take effect the changes.

Add username with hostname:

export PS1="\u\h "

Your prompt will now look like below:

skubuntuserver 

Add username and FQDN (Fully Qualified Domain Name):

export PS1="\u\H "

Add extra characters between username and hostname:

If you want to any letter, for example @, between the username and hostname, use the following entry:

export PS1="\u@\h "

The bash prompt will look like below:

sk@ubuntuserver

Add username with hostname with $ symbol at the end:

export PS1="\u@\h\\$ "

Add special characters between and after username and hostname:

export PS1="\u@\h> "

This entry will change the BASH prompt as shown below.

sk@ubuntuserver>

Similarly, you can add other special characters, such as colon, semi-colon, *, underscore, space etc.

Display username, hostname, shell name:

export PS1="\u@\h>\s "

Display username, hostname, shell and and Bash version:

export PS1="\u@\h>\s\v "

Bash prompt output:

Display username, hostname, shell and and Bash version in Bash prompt
Display username, hostname, shell and and Bash version in Bash prompt

Display username, hostname and path to current directory:

export PS1="\u@\h\w "

You will see a tilde (~) symbol if the current directory is $HOME.

Display date in BASH prompt:

To display date with your username and hostname in the BASH prompt, add the following entry in ~/.bashrc file.

export PS1="\u@\h>\d "
Display date in BASH prompt
Display date in BASH prompt

Date and time in 12 hour format in BASH prompt:

export PS1="\u@\h>\d\@ "

Date and 12 hour time hh:mm:ss format:

export PS1="\u@\h>\d\T "

Date and 24 hour time:

export PS1="\u@\h>\d\A "

Date and 24 hour hh:mm:ss format:

export PS1="\u@\h>\d\t "

These are some common escape sequences to change the Bash prompt format. There are few more escape sequences are available. You can view them all in in the bash man page under the "PROMPTING" section.

And, you can view the current prompt settings at any time using command:

$ echo $PS1

Hide "username@hostname" Part In Bash prompt:

I don't want to change anything. Can I hide it altogether? Yes, you can!

If you're a blogger or tech writer, there are chances that you have to upload the screenshots of your Linux Terminal in your websites and blogs.  Your username/hostname might be too cool, so you may not want others to copy and use them as their own. On the other hand, your username/hostname might be too weird or too bad or contain offensive characters, so you don't want others to view them. In such cases, this small tip might help you to hide or modify "username@hostname" part in Terminal.

If you don't like to let the users to view your username/hostname part, just follow the steps given below.

Edit your ~/.bashrc file:

$ vi ~/.bashrc

Add the following at the end:

PS1="\W> "

Type :wq to save and close the file.

Then, run the following command to take effect the changes.

$ source ~/.bashrc

That's it. Now, check your Terminal. You will not see the username@localhost part. You will only see the ~> symbol.

Hide "username@hostname" Part in Bash prompt
Hide "username@hostname" Part in Bash prompt

Warning: This is a bad practice in some cases. For example, if another shells like zsh inherits your current shell, it will cause some problems. Use it only for hiding or modifying your username@localhost part if you use single shell. Apart from hiding the username@localhost part in the Terminal, this tip is pretty useless and might be problematic.

Want to know another simplest way without messing the ~/.bashrc file? Just create another user account something like user@example, or admin@demo. Use these accounts for making guides, videos and upload them on your blog or online. Now, you have nothing to worry about your identity.

Colorize BASH Prompt

What we have seen so far is we just changed and added some elements to the BASH prompt. In this section, we are going to add colors the elements to the BASH prompt.

You can enhance the foreground (text) and background color of BASH prompt's elements by adding some code to the ~/.bashrc file.

For example, to change the foreground color of all texts to Red, add the following code:

export PS1="\u@\[\e[31m\]\h\[\e[m\] "

Once added, update the changes using command:

Now, your BASH prompt will look like below:

Colorize BASH prompt
Colorize BASH prompt

Similarly, to change the background color, add this code:

export PS1="\u@\[\e[31;46m\]\h\[\e[m\] "
Change the background color of bash prompt
Change the background color of bash prompt

Adding Emojis

Who doesn't love emoji? We can add an emoji by placing the following code in the ~/.bashrc file.

PS1="\W ? >"

Please note that some terminal may not show the emojis properly depending upon the font used. You may see either garbled characters or monochrome emoji if you don't have suitable fonts.

Online Bash Prompt Customizer

I find it bit difficult to Customize BASH prompt. Is there any other easy way? Yes!

If you're a newbie, writing and adding PS1 values will be confusing and bit difficult. Also, you will find it bit difficult to arrange the elements to get the result of your choice. No worries! There is an online Bash PS1 generator available which allows you to easily generate different PS1 values as you wish.

Go to the following website:

EzPrompt

Just pick the elements you want to use in your BASH prompt. Add the colors to the elements and re-arrange them in any order of your liking. Preview the output instantly and finally copy/paste resulting code in your ~/.bashrc file. It is that simple! Most of the examples mentioned in this guide are taken from this website. As already stated, don't forget to backup your current ~/.bashrc file before making any changes.


Suggested Read:


Restore bashrc File to Default Settings

I messed up with my .bashrc file? How to restore it to default settings? yes, it is possible!

As I mentioned earlier, it is strongly recommended to take backup ~./bashrc (Or any important configuration files in general) before making any changes. So, you can restore it to the previous working version if something went wrong.

However if you forgot to backup the ~/.bashrc file in the first place, you still can restore it to the default settings as described in the following guide.

The above guide is based on Ubuntu, but it may applicable to other Linux distributions as well. Please let us be clear that the aforementioned guide will help you to reset ~/.bashrc to its default settings at the time of new installation. Any changes done afterwards will be lost.

Resource:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More