Customizing and colorizing Linux bash prompt

Rate this post



Sometimes you get bored with the way the bash prompt looks like and you want to customize it by adding some useful features or changing colors. The default Bash prompt usually ends with a dollar sign $. Bash itself will show its major and minor version (\s-\v\$), for example, bash-3.00$. Most Linux distributions redefine the prompt to include additional information, such as your current login and computer, which is useful when you’re moving between accounts and computers. In order to customize the bash prompt we need to modify the $PS1 and $PS2 environment variables by our desire.

Bash will use the value of the $PS1 variable for your main prompt. If you include variable names in the string, Bash will substitute the value of the variables into your prompt. Bash has a PS2 (prompt string 2) variable, which is the prompt for incomplete command lines such as when you use a multiline quotation. By default, this prompt is a greater-than sign (>).

Customizing the prompt

Bash allows the customization of $PS1 and $PS2 prompt strings by inserting a number of backslash-escaped special characters. Below is the list of the most common used escape sequences. For full reference please see the manpage of bash command.

  • \h – displays the hostname (not the fully qualified domain name)
  • \H – displays the complete hostname, including the domain
  • \w – displays the current working directory ($HOME is abbreviated as a tilde “~” character)
  • \W – displays the basename of the current working directory ($HOME is abbreviated as a tilde “~” character)
  • \u – displays the username of the current user
  • \t – displays the current time in 24-hour HH:MM:SS format
  • \@ – displays the current time in 12-hour am/pm format
  • \! – displays the position of the command in the history list
  • \j – displays the number of jobs currently managed by the shell
  • \d – displays the date in “weekday-month-date” format
  • \# – displays the sequential command number for the session
  • \e – displays an ASCII escape character (033)
  • \$ – displays the default prompt (# for the superuser, otherwise $ for regular users)
  • \[ – begins a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
  • \] – ends a sequence of non-printing characters

Assuming the promptvars shell option is set, which it is by default, prompt strings are decoded, expanded via parameter expansion, command substitution, and arithmetic expansion, quotes are removed, and they are finally displayed.

Practical examples

In this section I will present some practical examples for customizing the bash prompt. In order to set the PS1 environment variable we must use the export command. To display the current prompt settings use the following command:

$ echo $PS1

This will display something like below:

\\u@\h \\W]\\$

export PS1=”\n[\u@\h jobs:\j]\$ “

This is useful if you run a lot of background jobs and forget that they are there.

export PS1=”\n[\u@\h \t \$?]\$ “

This will display the current time and the status of the last executed command. The bash prompt can be used to output the results of regular Linux commands.

export PS1=”\n[\u@\h $(ps -ef | wc -l)]\$ “

This example uses command substitution with the syntax $( or $() for displaying the total number of running processes.

  1. Displaying the number of jobs currently managed by the shell
  2. Displaying the time and the output of any linux command
  3. Displaying the number of total running processes

Note: It is recommended to put a space character as the last character in the $PS1 string because it makes it easier to read what is on your screen by separating the prompt string from the commands that you type.

Changing prompt colors

In order to display some more colorful fancy prompts we need to use non-printing escape sequences which have to be enclosed between \e[ and \]. For colour escape sequences, they should also be followed by a lowercase m. To change the default colors of your shell prompt use the following syntax:

‘\e[x;ym $PS1 \e[m’

  • \e[ – is the start color scheme
  • x;y – is the used color pair
  • $PS1 – is the shell prompt
  • \e[m – is the stop color scheme

In order to display a custom light blue prompt use the following command;

export PS1=”\e[1;34m[\u@\h \W]\$ \e[0m “

In this example, 1;34m means “set the character attribute to light, and the character color to blue.” 0m means “clear all attributes and set no color.” Below is the list of color codes which can be used in the bash prompt.

Foreround Color Code Foreground Color Code Background Color Code
Black 0;30 Dark gray 1;30 Black 0;40
Red 0;31 Light red 1;31 Red 0;41
Green 0;32 Light green 1;32 Green 0;42
Brown 0;33 Yellow 1;33 Yellow 0;43
Blue 0;34 Light blue 1;34 Blue 0;44
Purple 0;35 Light purple 1;35 Purple 0;45
Cyan 0;36 Light cyan 1;36 Cyan 0;46
Light gray 0;37 White 1;37 White 0;47

Another method to change bash prompt colors is by using the tput command. The tput utility has some capabilities for changing colors and few other bash prompt parameters. For example if we want to display an underlined yellow prompt we need to use the following syntax:

export PS1=”\[$(tput setaf 3 && tput smul)\]\u@\h:\w $ \[$(tput sgr0)\]”

Below is a list of some common tput capabilities:

  • tput setaf – sets the foreground colour using ANSI escape
  • tput setab – sets the background colour using ANSI escape
  • tput setf – sets the foreground colour
  • tput setb – sets the background colour
  • tput bold – sets the bold mode
  • tput smul – sets the underline mode
  • tput sgr0 – turns off all attributes

The following table displays the list of colors supported by the tput utility:

Color Code Color Code
Black 0 Blue 4
Red 1 Magenta 5
Green 2 Cyan 6
Yellow 3 White 7

For more informations please read the man pages of tput and terminfo commands. All these settings are valid only in the current session and are lost when we close the shell or reboot the system. In order to preserve these settings permanently we must place the “export line” for $PS1 string globally in /etc/bashrc or locally in ~/.bashrc. Keep in mind that the local bashrc file is hidden because it starts with a dot “.”. In order to see it use the “ls -la” command.


All these customizations allows us to keep track of system information and the most practical aspect of colourizing your prompt is the ability to quickly spot the prompt when you use scrollback. For more details regarding bash prompt customization see the BASH Prompt Howto

Leave a Reply

Your email address will not be published.