The “Bash prompt” on the command line interface is that bit of text that precedes your commands. It is usually the username followed by the hostname on most systems, so the syntax might look something like user@linuxconfig$ for example. Suffice it to say, the default Bash command line prompt on many Linux systems is quite minimal.
As we will see in this article, it can be easily changed by modifying the Bash PS{n} variables, and we can include information such as display time, load, number of users using the system, uptime and more. In this tutorial, you will see how to spruce up your Bash prompt with colors and additional information on a Linux system.
In this tutorial you will learn:
What are PS1 and PS2 shell variables
How to create custom shell prompts
What are the characters we can use to customize a shell prompt
Minimal knowledge of the Bash shell. Privileged access to your Linux system as root or via the sudo command.
Conventions
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command $ – requires given linux commands to be executed as a regular non-privileged user
Bash prompt variables
Just like everything else on a Linux system, so too can the Bash prompt also be customized. We can accomplish the task by changing the values of Bash PS1, PS2, PS3, and PS4 variables. To keep the things simple, this article will be concerned just with the first two. Use the echo command to see their current values:
This looks a little confusing, because a lot of it used to set the colors you see in the screenshot above. The takeaway here is the use of the special Bash characters \u (user), \h (hostname), and \w (current working directory). These types of characters are what allow us to display custom information which will change with the circumstances in the Bash prompt. More of them will be covered below.
Bash PS2 prompt variable
PS2 bash shell variable is the secondary prompt, and is usually set to >. This prompt is displayed if the shell waits for some user input, for example if you are using quotation marks and forget to close them.
Bash prompt can be customized by using special characters. Here is a quick overview of the most used characters and their meaning:
Bash prompt special characters
Bash special character
Bash special character explanation
Bash special character
Bash special character explanation
\a
an ASCII bell character (07)
\d
the date in “Weekday Month Date” format (e.g., “Tue May 26”)
\]
end a sequence of non-printing characters
\e
an ASCII escape character (033)
\h
the hostname up to the first `.’
\H
the hostname
\j
the number of jobs currently managed by the shell
\l
the basename of the shell’s terminal device name
\n
newline
\r
carriage return
\s
the name of the shell, the basename of $0 (the portion following the final
slash)
\t
the current time in 24-hour HH:MM:SS format
\T
the current time in 12-hour HH:MM:SS format
\@
the current time in 12-hour am/pm format
\A
the current time in 24-hour HH:MM format
\u
the username of the current user
\v
the version of bash (e.g., 2.00)
\V
the release of bash, version + patchelvel (e.g., 2.00.0)
\w
the current working directory
\W
the basename of the current working directory
\!
the history number of this command
\#
the command number of this command
\$
if the effective UID is 0, a #, otherwise a $
\nnn
the character corresponding to the octal number nnn
\\
a backslash
\[
begin a sequence of non-printing characters, which could be used to embed a
terminal control sequence into the prompt
\D{format}
the format is passed to strftime(3) and the result is inserted
into the prompt string; an empty format results in a locale-specific time
representation. The braces are required
Bash prompt customization
After a user logs into the system, the user environment variables are initialized from various files:
In the sections below, we will define two variables to prove this statement.
Customizing Bash variable step by step instructions
We will now edit the ~/.bashrc configuration file to display a custom Bash prompt. As mentioned above, we will need to logout of our current terminal session and log back in to notice the changes taking effect or just use the source command as shown below.
Use nano or some other text editor to edit the ~/.bashrc file. Alternatively, we can echo the new shell prompt settings into the file.
$ echo "PS1='MY NEW BASH PROMPT@\\t:\\w\\$ '" >> ~/.bashrc
Make the changes take effect with the following source command.
This bash prompt displays current number of files and directories in the current directory.
export PS1="\u@\h [\$(ls | wc -l)]:\$ "
Closing Thoughts
In this tutorial, we saw how to change the Bash prompt in the command line interface on a Linux system. Not only can this make our command line terminal look much better, but we can also use Bash’s special characters to display lots of useful information. Feel free to adapt some of our examples to your own needs, so you can get a nice looking Bash prompt with some helpful information at the same time.