The PS1 environment controls the appearance of the BASH command line prompt. There are a variety of default prompts but they usually include username, hostname, and working directory. You can easily customize your prompt to display information important to you as well as add color and style formatting.
Here we can see the default prompt is “username@hostname working_directory $”.
ryan@workstation ~ $
You can echo the PS1 environment variable and see the character codes which are used to create the output.
ryan@workstation ~ $ echo $PS1
\u@\h \w \$
Here is a list of all the special character codes you can use to build your PS1 command prompt.
Special Character Codes
\a – an ASCII bell character (07)
\d – the date in “Weekday Month Date” format (e.g., “Tue May 26”)
\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
\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
\] – end a sequence of non-printing characters
Lets try a simple change. Make sure you use double quotes so the escape characters get passed on to the variable and not expanded in the shell.
ryan@workstation ~ $ PS1="My Prompt: $ "
My Prompt: $
As you can see the prompt will immediately change to reflect the changes.
We can add the date to the prompt.
ryan@workstation ~ $ PS1="\d \u@\h \w \$ "
Sun Oct 19 ryan@workstation ~ $
Now lets add some color to our original prompt.
ryan@workstation ~ $ PS1="\[\e[1;32m\]\u@\h\[\e[1;34m\] \w \$\[\e[0m\] "
Let go over this in detail.
A style block will display all text that follows with the style it defines. The block contains 3 elements.
Lets look at the first block.
“\[\e[1;32m\]”
“\[” – begin a sequence of non-printing characters
“\e[1;32m” – the semicolon separated list of style/color codes (in this case bold;green)
“\]” – end a sequence of non-printing characters
The first block will display the following username@hostname text in BOLD green.
“\[\e[1;32m\]\u@\h\[\e[1;34m\] \w \$\[\e[0m\] ”
The second block will display the following working_directory text in BOLD blue.
“\[\e[1;32m\]\u@\h\[\e[1;34m\] \w \$\[\e[0m\] ”
The final block resets the colors.
“\[\e[1;32m\]\u@\h\[\e[1;34m\] \w \$\[\e[0m\] ”
Here is a rundown of all the color and style codes.
Color and Style Codes
Style
0 – default
1 – bold
4 – underline
7 – inverse
9 – strikeout
Foreground Colors
30 – foreground Black
31 – foreground Red
32 – foreground Green
33 – foreground Yellow
34 – foreground Blue
35 – foreground Magenta
36 – foreground Cyan
37 – foreground White
Background Colors
40 – background Black
41 – background Red
42 – background Green
43 – background Yellow
44 – background Blue
45 – background Magenta
46 – background Cyan
47 – background White
You can combine multiple codes to create the exact style you want.
“\[\e[1;4;36;47m\]”
This style block defines bold and underlined cyan text with a white background.
To make these changes permanent place your new prompt string like the one below in your ~/.bashrc file in your home directory.
PS1="\[\e[1;32m\]\u@\h\[\e[1;34m\] \w \$ \[\e[0m\]"
This file is sourced every time you start a new interactive shell.