Categories
Self-organization

How to customize (and colorize) your bash prompt

How to customize (and colorize) your bash prompt

H ow do I change the color of my shell prompt under Linux or Unix operating systems? How do I customize and colorize my Bash prompt (PS1) on a Linux, macOS or Unix-like system?

You can change the color of your shell prompt to impress your friend or to make your own life quite easy while working at the command prompt. BASH shell is the default under Linux and Apple OS X. Your current prompt setting is stored in a shell variable called PS1. There are other variables too, like PS2, PS3 and PS4. Let us see how to change the color of shell prompt on a Linux or Unix system when using bash.

Tutorial details
Difficulty level Easy (rss)
Root privileges No
Requirements Bash
Est. reading time 5m

Bash displays the primary prompt PS1 when it is ready to read a command, and the secondary prompt PS2 when it needs more input to complete a command. Bash allows these prompt strings to be customized by inserting a number of backslash-escaped special characters.

Task: Displaying current BASH prompt (PS1)

Use the echo command/printf command to display current BASH prompt settings:
$ echo “$PS1”
## OR ##
$ printf “%s\n” “$PS1”
Here is what I see:

Here is another output from my Debian based system:
$ echo $PS1
Ubuntu/Debian based Linux distro displayed the following:

By default the command prompt is set to [\[email protected]\h \W]\$ . The backslash-escaped special characters are decoded as follows:

  • \u : Display the current username .
  • \h : Display the hostname
  • \W : Print the base of current working directory.
  • \$ : Display # (indicates root user) if the effective UID is 0, otherwise display a $.

Task: Modifing current BASH prompt

Before you modify settings save your old prompt using the following command:
oldps1=”$PS1″
So if you messed up, you can switch back easily using the following syntax:
PS1=”$oldps1″
Use the export command to setup a new shell prompt:
$ export PS1=”[\\[email protected]\\H \\W \\@]\\$ ”

Fig.01: New prompt in action

  • \H : Display FQDN (fully qualified domain name) hostname.
  • \@ : Display current time in 12-hour am/pm format.

Task: Adding colors to the prompt

To add colors to the shell prompt use the following export command syntax:
‘\e[x;ym $PS1 \e[m’
Where,

  • \e[ : Start color scheme.
  • x;y : Color pair to use (x;y)
  • $PS1 : Your shell prompt variable.
  • \e[m : Stop color scheme.

Change the color of shell prompt by setting the PS1

To set a red color prompt, type the following export command:
$ export PS1=”\e[0;31m[\[email protected]\h \W]\$ \e[m ”

How to customize (and colorize) your bash prompt

Fig.02: Adding the colors to the prompt

A list of color codes

Color Code
Black 0;30
Blue 0;34
Green 0;32
Cyan 0;36
Red 0;31
Purple 0;35
Brown 0;33
Blue 0;34
Green 0;32
Cyan 0;36
Red 0;31
Purple 0;35
Brown 0;33

Note: You need to replace digit 0 with 1 to get light color version.