11-06-2019, 09:05 AM
*This tutorial is part of a series, visit the main thread to learn more*
When we own a server we basically interact with it via a terminal using SSH or something older then that. The bash prompt became something that we always see and read, so why don't customize it?
On this tutorial, we will learn how customize our bash prompt: how to change colors and how to add information that can useful on Ubuntu Server & CentOS.
Let's start with some information!
When we own a server we basically interact with it via a terminal using SSH or something older then that. The bash prompt became something that we always see and read, so why don't customize it?
On this tutorial, we will learn how customize our bash prompt: how to change colors and how to add information that can useful on Ubuntu Server & CentOS.
Let's start with some information!
- How works our bash prompt?
Most Linux distro displays by default the hostname and the current working directory. Prompt's configuration is stored inside a special shell variable called PS1, there are other variables called PS2, PS3 and PS4 that we will not be used on this tutorial.
So we can say that the PS1 variable is used as the primary prompt string and it is evaluated before each terminal update. To see our current bash's prompt configuration we can run the following command:
This configuration can be system-wide or user-specific, on this tutorial we will focus on user-specific customization.Code: (Select All)echo $PS1
- Where is placed the bash's prompt configuration file?
As we said above, the customization we will focus on is the user-specific one, so our bash prompt configuration file called ".bashrc", that contains the PS1 variable, is stored in the user's home:
Code: (Select All)~/.bashrc
OR
/home/{user}/.bashrc - What kind of customization can I configure?
Well, let's start listing the customization that can add useful information to our bash:- A bell character:Code: (Select All)
\a
- An escape character:Code: (Select All)
\e
- A newline:Code: (Select All)
\n
- A carriage return:Code: (Select All)
\r
- A backslash:Code: (Select All)
\\
- A character whose ASCII code is the octal value nnn:Code: (Select All)
\nnn
- A character (like 'q' will print 'q'): Code: (Select All)
{character}
- If the user's uid is 0 it will print a '#', otherwise, if the user is 'root' it will print a '$':Code: (Select All)
\$
- The date in 'Weekday Month Date' format (like “Tue May 26”):Code: (Select All)
\d
- The time, in 24-hour HH:MM:SS format:Code: (Select All)
\t
- The time, in 12-hour HH:MM:SS format:Code: (Select All)
\T
- The time, in 12-hour am/pm format: Code: (Select All)
\@
- The time, in 24-hour HH:MM format:Code: (Select All)
\A
- The username of the current user:Code: (Select All)
\u
- The version of Bash (like 5.00):Code: (Select All)
\v
- The release of Bash, version and patchlevel (like 5.00.2):Code: (Select All)
\V
- The hostname, up to the first ‘.’ (like 'POST4VPS.FREEVPS.COM' will be displayes as 'POST4VPS'):Code: (Select All)
\h
- The hostname (like 'POST4VPS.FREEVPS.COM' will be displayes as 'POST4VPS.FREEVPS.COM'):Code: (Select All)
\H
- The number of jobs currently managed by the shell:Code: (Select All)
\j
- The basename of the shell’s terminal device name:Code: (Select All)
\l
- The name of the shell, the basename of $0 (the portion following the final slash):Code: (Select All)
\s
- The current working directory, with $HOME abbreviated with a tilde (like if you are on '/home/POST4VPS_tutorial' folder it will print '/home/POST4VPS_tutorial'):Code: (Select All)
\w
- The basename of $PWD, with $HOME abbreviated with a tilde (like if you are on '/home/POST4VPS_tutorial' folder it will print 'POST4VPS_tutorial'):Code: (Select All)
\W
- The history number of this command:Code: (Select All)
\!
- The command number of this command:Code: (Select All)
\#
- Begin a sequence of non-printing characters that can be used to embed a terminal control sequence into the prompt:Code: (Select All)
\[
- End a sequence of non-printing characters:Code: (Select All)
\]
- Reset the colors:Code: (Select All)
\e[0m
- Set text color as Black:Code: (Select All)
\e[0;30m
- Set text color as Red:Code: (Select All)
\e[0;31m
- Set text color as Green:Code: (Select All)
\e[0;32m
- Set text color as Yellow:Code: (Select All)
\e[0;33m
- Set text color as Blue:Code: (Select All)
\e[0;34m
- Set text color as Purple:Code: (Select All)
\e[0;35m
- Set text color as Cyan:Code: (Select All)
\e[0;36m
- Set text color as White:Code: (Select All)
\e[0;37m
- Set text as bold and text's color as Black:Code: (Select All)
\e[1;30m
- Set text as bold and text's color as Red:Code: (Select All)
\e[1;31m
- Set text as bold and text's color as Green:Code: (Select All)
\e[1;32m
- Set text as bold and text's color as Yellow:Code: (Select All)
\e[1;33m
- Set text as bold and text's color as Blue:Code: (Select All)
\e[1;34m
- Set text as bold and text's color as Purple:Code: (Select All)
\e[1;35m
- Set text as bold and text's color as Cyan:Code: (Select All)
\e[1;36m
- Set text as bold and text's color as White:Code: (Select All)
\e[1;37m
- Set text as underline and text's color as Black:Code: (Select All)
\e[4;30m
- Set text as underline and text's color as Red:Code: (Select All)
\e[4;31m
- Set text as underline and text's color as Green:Code: (Select All)
\e[4;32m
- Set text as underline and text's color as Yellow:Code: (Select All)
\e[4;33m
- Set text as underline and text's color as Blue:Code: (Select All)
\e[4;34m
- Set text as underline and text's color as Purple:Code: (Select All)
\e[4;35m
- Set text as underline and text's color as Cyan:Code: (Select All)
\e[4;36m
- Set text as underline and text's color as White:Code: (Select All)
\e[4;37m
- Set the text background color as Black:Code: (Select All)
\e[40m
- Set the text background color as Red:Code: (Select All)
\e[41m
- Set the text background color as Green:Code: (Select All)
\e[42m
- Set the text background color as Yellow:Code: (Select All)
\e[43m
- Set the text background color as Blue:Code: (Select All)
\e[44m
- Set the text background color as Purple:Code: (Select All)
\e[45m
- Set the text background color as Cyan:Code: (Select All)
\e[46m
- Set the text background color as White:Code: (Select All)
\e[47m
Now, we can start our tutorial:
Opening file configuration:- MAKE A BACKUP of your .bashrc before starting editing it!
- As we already said our bash prompt configuration file is located inside the current user folder, so we can open it using terminal editor, such as nano or vim, or graphical editors, such as gedit or sublime.
Locate the PS1 variable and start customizing:- Scroll down until you locate the PS1 variable.
- Add your own customization by mixing up all the above snippets of code.
Examples:- I want to display username in yellow and hostname in red (Desired output: user@host: Code: (Select All)
\[\e[0;33m\]\u\[\e[0m\]@\[\e[0;31m\]\h[\e[0m\]
- I want to display the username is blue, hostname in purple and the current directory in bold and green: Code: (Select All)
\[\e[0;34m\]\u\[\e[0m\]@\[\e[0;35m\]\h[\e[0m\]\[\e[4;32m\]\w
Useful online tools:
There are very useful online tools that can help you creating your own bash configuration:
To see your customization enable just re-log into the system!
Have fun! - A bell character: