The PS1 environment variable holds the format for the prompt. It may be
set in a variety of ways but the standard is in the .bashrc for linux.
There is two additional environment variables that fall within the scope
of this document: PS2 and PS3. The PS2 prompt is a secondary prompt used
when the system is calling for more input to complete a command. The PS3
prompt is the next level of inquiry from the system when additional input
is required.
The Bash shell allows these prompt strings to be customized by inserting a
number of backslash-escaped special characters that are decoded as follows:
\a an ASCII bell character (07)
\d the date in "Weekday Month Date" format
(e.g., "Tue May 26")
\e an ASCII escape character (033)
\h the hostname up to the first `.'
\H the hostname
\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
\u the username of the current user
\v the version of bash (e.g., 2.00)
\V the release of bash, version + patchlevel
(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 con-
trol sequence into the prompt
\] end a sequence of non-printing characters
An example of setting the PS1 environment variable on the command line
is as follows:
% export PS1='$ '
This will set the command prompt to be a dollar sign ($ ). You will use the
same syntax in the .bashrc configuration file.
You may also execute commands with the command prompt. The PS1 environment
variable can be set using $() to execute commands when the prompt is
generated.
% PS1="[\$(date +%H%M)]\$ "
[2018]$
Note: It's important to notice the backslash before the dollar
sign of the command substitution. Without it, the external command
is executed exactly once: when the PS1 string is read into the
environment. For the above prompt, it would display the same time
no matter how long the prompt was used. The backslash protects the
contents of $() from immediate shell interpretation, so "date" is
called every time a prompt is generated.
Bash provides another environment variable called PROMPT_COMMAND.
The contents of this variable are executed as a regular Bash command
just before Bash displays a prompt.
PROMPT_COMMAND="date +%H%M"