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.
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.
Show username only
To show only the username, just change the previous line to the following:
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
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:
export PS1="\u@\h "
Add username with hostname and $ symbol at the end
export PS1="\u@\h\\$ "
Add special characters between and after username and hostname
export PS1="\u@\h> "
Similarly, other special characters can be added, such as a colon, semicolon, *, underscore, space, etc.
Show username, hostname, shellname
export PS1="\u@\h>\s: "
Show username, hostname, shell and its version
export PS1="\u@\h>\s\v "
Show username, hostname and path to current directory
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.
export PS1="\u@\h>\d "
Date and time in 12-hour format in BASH
export PS1="\d> \@ > "
Date and time 12 format 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:
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:
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:
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.
How to change the color of the prompt when starting as super user
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.
Hello my Linux terminal does not show me the prompt and therefore I cannot write anything that I can do.