Prompt, some examples to customize it to your liking

In the next article we will take a look at how modify the prompt of our Ubuntu. We already talked about this in this same blog some time ago, but this time we are going to add some more possibilities to customize the terminal.

BASH (Bourne-again shell) is the default shell for most modern Gnu / Linux distributions. In the following lines we are going to customize the BASH indicator and improve its appearance by adding some colors, styles, modifying elements, etc. All of it without having to install tools, add-ons or resort to online services.

Customize the bash prompt

In BASH, we can customize and change the prompt in any way we want. There will only be change environment variable value PS1. Each machine will see a different username and hostname.

default bash prompt

On the machine where I'm going to test these examples, entreunosyceros is my username and 18-04 is my hostname. Now we are going to change this prompt by inserting some special characters called escape sequences.

Before I go on and change things, it is always advisable to make a backup copy of the file ~/.bashrc.

cp ~/.bashrc ~/.bashrc.bak

Modify 'username @ hostname' at the prompt

In this example we are going to replace the 'username @ hostname' part with 'Hello @ welcome>'.

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

export PS1="Hola@bienvenido> "

Once finished, save the file and return to the terminal. Don't forget to update the changes with the command 'source ~. / Bashrc'.

Here is the output from the prompt on my Ubuntu 18.04 LTS.

bash prompt hello welcome

Show username only

To show only the username, just change the previous line to the following:

bash prompt username only

export PS1="\u "

Here, \ u is the escape sequence.

There are a few more values ​​to add to the PS1 variable to change this. Remember that after adding a change, we must execute the command 'source ~/.bashrc'for the changes to take effect.

Add username with hostname

bash prompt user and host

export PS1="\u> \h> "

Add username and FQDN

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

bash prompt username and FQDN

export PS1="\u@\h "

Add username with hostname and $ symbol at the end

bash prompt username, hostname and dollar symbol

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

Add special characters between and after username and hostname

bash prompt special characters between and after user and host man

export PS1="\u@\h> "

Similarly, other special characters can be added, such as a colon, semicolon, *, underscore, space, etc.

Show username, hostname, shellname

bash prompt username, host and shell name

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

Show username, hostname, shell and its version

bash prompt username, hostname and shell version

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

Show username, hostname and path to current directory

bash prompt username, hostname, and directory path

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

You will see the ~ symbol if the current directory is $ HOME.

Display date in BASH message

For show the date with your username and hostname at the prompt, add the following in the file ~/.bashrc.

bash prompt username, hostname and date

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

Date and time in 12-hour format in BASH

bash prompt date and time at 12 hours

export PS1="\d> \@ > "

Date and time 12 format hh: mm: ss

bash prompt date and time 12 hours hh: mm: ss

export PS1="\d> \T> "

Date and time 24 hours

export PS1="\d> \A> "

Date and time in 24 hour format hh: mm: ss

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

These are just some common escape sequences to change the format of the bash message. There are a few more sequences available. You can see them all in the bash man page.

At any time you can view current settings of the $ PS1 variable by typing in the terminal the command:

see bash prompt configuration

echo $PS1

Coloring the prompt

What we have seen so far is that we just changed / added some items to the bash prompt. Now we are going to modify the colors of some elements.

To give a touch of color to the foreground (text) and background color to the elements, as before we will add a code to the ~ / .bashrc file.

For example, to change the color of the text to the hostname and make it look red, we will add the following code:

bash prompt host text color in red

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

Once added, you have to update changes with command in terminal:

source ~/.bashrc

Similarly, for change background color to hostname, the code to use would be this:

bash prompt text and background color

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

Restore default settings

As I mentioned at the beginning, it is always recommended to backup the file ~. / bashrc before making changes. Therefore, you can restore to the previous working version if something goes wrongl. You will only have to replace the ~ / .bashrc file with the backup one.


Leave a Comment

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

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Leon S. said

    How to change the color of the prompt when starting as super user

    1.    Damien Amoedo said

      Hello. Log into the terminal as root. Go to the / root directory. There you will find another .bashrc file. Edit it and modify the variable PS1 as indicated in the article or as you prefer. Save the changes and make a source ~ / .bashrc. Salu2.

  2.   Caro said

    Hello my Linux terminal does not show me the prompt and therefore I cannot write anything that I can do.