How to customize the command prompt in bash

Updated: 12/29/2017 by Computer Hope

In the bash shell, the command prompt is the text shown when you are being prompted for input. On most systems, it looks like this:

a standard command prompt in bash

In the prompt, you can have information before you run any commands. For example, the prompt above shows you the following.

  1. who you are (a user named user)
  2. where you are (a computer named myhost)
  3. what your working directory is (~, which is an abbreviation for your home directory)

Perhaps most important is the dollar sign ("$"), which is a special character that indicates you are logged in as a non-root user. If you were currently logged in as root, this character would be a pound sign ("#") instead.

The string representing the prompt is stored in the special environment variable named PS1. For the prompt above, its value looks like the following.

"\[email protected]\h:\W\$ "

There are four special characters in this string: \u, \h, \W, \$. These are escaped character sequences that are replaced with actual values every time the prompt is displayed.

Special characters

The special character sequences listed in the table below can be used in the bash shell prompt.

Character sequence Translates to
\a The "alarm" character. Triggers a beep or a screen flash
\d The current date, displayed in the format Weekday Month Date (e.g., Wednesday May 13).
\D{format} The current date and time displayed according to format as interpreted by strftime. If format is omitted, \D{} displays current 12-hour A.M./P.M. time (e.g., 07:23:01 PM).
\e An escape character (ASCII 27)
\e[numberm Denotes the beginning of a sequence to display in color, where number is a number, or pair of numbers, which specifies what color and style to use. See below for a list of colors and their number pairs.
\e[m Denotes the end of a sequence to display in color.
\h The hostname of the machine, up to the first "." For instance, if the system's hostname is myhost.mydomain, \h displays myhost
\H The full hostname of the machine.
\j Number of jobs being managed by the shell.
\l The shell's terminal device identifier, usually a single-digit number.
\n A newline
\r Carriage return
\s The name of the shell (the basename of the process that initiated the current bash session).
\t Current time displayed in 24-hour HH:MM:SS format (e.g., 19:23:01).
\T Current time displayed in 12-hour HH:MM:SS format (eg. 07:23:01).
\@ Current time displayed in 12-hour HH:MM:SS A.M./P.M. format (e.g., 07:23:01 P.M.).
\A Current time in 24-hour HH:MM format, (e.g., 19:23).
\u The username of the current user.
\v Bash version number (e.g., 4.3).
\V Bash version and patch number (e.g., 4.3.30).
\w The current directory. The user's home directory is abbreviated as a tilde ("~"). For example, /usr/bin, ~, or ~/documents
\W The basename of the current working directory (e.g., bin, ~, or documents).
\! The history number of the current command.
\# The command number of the current command (command numbers are like history numbers, but they reset to zero when you start a new bash session).
\nnn The ASCII character whose octal value is nnn.
\\ A backslash
\[ Marks the beginning of any sequence of non-printing characters, such as terminal control codes.
\] Marks the end of a non-printing sequence.
\$ Print # if UID is zero (superuser), or $ if greater than zero (anyone else).

Changing the prompt for the current session

You can set the PS1 environment variable as you would any other variable in bash, with the statement form NAME=VALUE. So to set the standard prompt, you could type the following at the command line.

PS1="\[email protected]\h:\W\$ "

…And your prompt will immediately change. Notice the final space after the dollar sign in the string for an extra space after your prompt, which makes it easier to differentiate the commands you type from the prompt. The extra space isn't necessary, but it's recommended.

Notice that in the command, there are no spaces between PS1, =, and the string.

Changing the prompt for all future sessions

If you want to change the prompt for every new session, you need to edit your bash startup files. Every time you start a terminal session, the following files, if they exist, are read in and executed as scripts:

/etc/profile
~/.bash_profile
~/.bash_login
~/.profile

The first file, /etc/profile, is a startup script for any user on the system who starts a bash session. To change the prompt for every user, you need to edit this file, and you need superuser privileges to do so.

sudo vi /etc/profile

For example, the above command will prompt you for your password, and if you are on the sudoers list, open the universal bash startup file as the superuser, using the vi editor.

The other three files are looked for in the order listed above and executed after /etc/profile. These three files live in your home directory, so they are executed only for you. They may define PS1 in more than one place, so you should read the files carefully to see how they're configured on your system. For instance, many systems will use a different PS1 value depending on whether or not your terminal supports color.

Any of the three files may exist and be used for your bash session, but if there is more than one, they will be executed in the order listed.

If you don't want to delete or change lines in your startup files, you can also add a new PS1= statement to the end of one of your files to apply a custom prompt.

Adding color to your prompt

If you're using a terminal that supports color (and you probably are), you can add color to your prompt. Colors are added using special character sequences that follow this format:

\e[numberm Begins a colored section. The number value is a special number, or pair of numbers, which specifies the color.
\e[m denotes the end of the colored section; resets to default color

For instance, the prompt string:

PS1="\e[1;35mThis is bright purple. \e[0;35m...and this is dark purple.\e[m "

Will produce a prompt that looks like this:

bash prompt with color

Here is a list of the colors you can use, and the numbers that specify them:

Number(s) Color/Style Number(s) Color/Style
0;30 Black 1;30 Dark Gray
0;31 Red 1;31 Bright Red
0;32 Green 1;32 Bright Green
0;33 Brown 1;33 Yellow
0;34 Blue 1;34 Bright Blue
0;35 Purple 1;35 Bright Purple
0;36 Cyan 1;36 Bright Cyan
0;37 Gray 1;37 White
4;30 Black, underlined 40 background: Dark Gray
4;31 Red, underlined 41 background: Red
4;32 Green, underlined 42 background: Green
4;33 Brown, underlined 43 background: Yellow
4;34 Blue, underlined 44 background: Blue
4;35 Purple, underlined 45 background: Purple
4;36 Cyan, underlined 46 background: Cyan
4;37 Gray, underlined 47 Background: Gray

Here are some examples, and what they look like:

PS1="\e[0;31mRed \e[0;32mGreen \e[0;33mBrown \e[0;34mBlue \e[0;35mPurple \e[0;36mCyan \e[0;37mGray \e[m"
bash color prompt example 2
PS1="Underlined: \e[4;31mRed \e[4;32mGreen \e[4;33mBrown \e[4;34mBlue \e[4;35mPurple \e[4;36mCyan \e[4;37mGray \e[m"

bash color prompt example 3

PS1="\e[44m\e[1;35mBright purple on a blue background\e[m "

bash color prompt example 4