Page Body

Customize the Command Prompt With the PS1 Shell Variable

There are several Prompt Statement (PS) shell variables. The PS1 variable is used to set the format of the command line interface's (CLI) prompt.

Prompt Macros

Prompt statement variables, like PS1, can use various prompt macros. These macros are shorthand notations that expand to useful pieces of information that may be displayed at the command line.

These are some of the most helpful prompt macros:

\a
ASCII bell character (07).
\A
Current time in 24-hour format (HH:MM).
\d
Date in weekday, month, date format (e.g., Tue May 26).
\D {ex_format}
The provided format is passed to strftime (i.e., a command that formats the date and time) and the result is inserted into the prompt string. An empty ex_format results in a locale-specific time representation. The braces are required.
\e
ASCII escape character (033).
\h
Hostname up to the first ..
\H
Hostname.
\j
Number of jobs currently managed by the shell.
\l
Basename of the shell's terminal device name.
\n
Newline.
\r
Carriage return.
\s
Name of the shell. Equivalent to the value of the ${0} special parameter.
\t
Current time in 24-hour format (HH:MM:SS).
\T
Current time in 12-hour format (HH:MM:SS).
\@
Current time in 12-hour am/pm format.
\u
Username of the current user.
\v
Version of bash (e.g., 2.00).
\V
Release of bash, i.e., version and patch level (e.g., 2.00.0).
\w
Current working directory, with ${HOME} abbreviated with a tilde (~).
\W
Basename of the current working directory, with ${HOME} abbreviated with a tilde (~).
\!
History number of this command, i.e., the command's position in the history stack.
\#
Command number of this command, i.e., the command's position in the current shell session.
\$
If effective UID is 0, a #, otherwise a $. Represents the prompt itself.
\nnn
Character corresponding to the octal number nnn.
\\
Backslash.
\[
Begin a sequence of non-printing characters. Can be used to embed a terminal control sequence into the prompt.
\]
End a sequence of non-printing characters.

These special characters must be surrounded in single quotes when they are used on the command line.

Creating a Custom Command Prompt

First, you need to decide the format that you want your command prompt to take.

For this example, we want to include:

  • The date in weekday, month, date format
  • The current user's username
  • The hostname up to the first .
  • The current working directory
  • A symbol for the prompt itself
  • A space after the symbol representing the command prompt

When we put this all together, we get:

\d \u@\h:\w\$ 

In order to make this change persistent across non-login interactive shell sessions, you can run the following command:

$ {
    echo -e '\n# Set custom command prompt'
    echo "PS1='\d \u@\h:\w\$ '"
} >> "${HOME}/.bashrc"

The code above has a code block that echoes a blank line, a comment line, and then a line that sets the PS1 variable to our new specification. Finally, the code block command results are appended to the current user's .bashrc file.

You can either source your user's .bashrc file (i.e., source "${HOME}/.bashrc"), or close and re-open your non-login interactive shell session to see the change. Most likely, your shell's login configuration file is sourcing your user's .bashrc file, so you will see your command prompt change the next time you log into your system, as well.

Coloring Your Command Prompt

Some GNU/Linux distributions color the text of their command prompt to make it more legible. You can accomplish this with Bash escape characters and Bash colors.

Show/Hide Bash Color Codes
Bash Color Codes
Color Code Color Code
Black 0;30 Dark Gray 1;30
Red 0;31 Light Red 1;31
Green 0;32 Light Green 1;32
Brown 0;33 Yellow 1;33
Blue 0;34 Light Blue 1;34
Purple 0;35 Light Purple 1;35
Cyan 0;36 Light Cyan 1;36
Light Gray 0;37 White 1;37

To color your prompt macros, you can surround them with the Bash escape character, \e[m. Then, specify a color code between the \e[ and the first m. For example, e[1;30m\d\e[m colors the weekday, month, date format macro (\d) as dark gray (1;30).

This is how to specify the prior customized command prompt example, with colors:

\e[1;30m\d\e[m \e[1;31m\u\e[m@\e[1;34m\h\e[m:\e[1;30m\w\e[m\$ 

The result looks like:

Command Prompt With Colors

For more information on how escape characters are used to provide colors in terminals, check out the following resources:

Further Resources

To learn more about prompt macros, run the man 1 bash command (PROMPTING section) or check out the Bash Reference Manual.

Enjoyed this post?

Subscribe to the feed for the latest updates.