How to Change Kali Linux Terminal Prompt Color

The environment variable PS1 (Prompt String One) holds the string that is being displayed as a command or shell prompt in the terminal emulator program Terminal. The prompt string is embedded (interspersed) with color tags in the form of

\[\033[x;ym\] 

where x is a display attribute (normal, dim, bold, italics, etc) and y is the color code.

Changing the value or content of PS1 during a terminal session quickly changes the displayed prompt, however, the changes are lost once the terminal session is closed. To make the changes permanent, the hidden file named .bashrc located in the user’s home directory must be edited to reflect the changes in the prompt string.

Displaying the Value of PS1

Command

echo $PS1

Output

\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;33m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$

Changing the Prompt

The prompt string can be made as simple as a single character like a dollar sign ($) or a hashtag sign (#). Or it can be any text string you may want. To change the prompt string, assign a new value to PS1.

To enter a string with spaces or non-printable characters, enclose the string with single (‘) or double (“) quotes.

PS1=$
PS1=#
PS1=Jean
PS1="Jean Valjean"
PS1=24601
Figure 1 – Examples of Customized Prompt

The Default Shell Prompt

The default shell prompt, besides being a cue for entering a shell command in a terminal session, is also used to convey a lot of information about the system. Take for an example the default Kali Linux shell prompt,

root@kali~#
Figure 2 – Default Kali Linux Terminal Shell Prompt

The default shell prompt is in red, a warning sign. It indicates that the terminal user has administrative privileges or root privileges. It means that the user may, and can, mess up the system with improper commands like deleting files necessary for Kali’s normal operations.

It also shows the current user root, the host name kali, the current working directory, the home directory represented by the tilde sign (~), and finally, the hashtag sign (#) indicating a priveleged user as opposed to an ordinary user which is usually handed a prompt with a dollar sign ($).

Understanding the Cryptic PS1

To incorporate meaningful information on the shell prompt, bash, the shell interpreter, uses a special set of characters called escape characters , to encode information in the prompt string. See the table (Bash Prompt Escape Characters) below for the complete list of escape characters that are recognized by bash.

Escape Character CodeMeaning
\uUsername
\hHostname
\wWorking directory
\$Display a hashtag if uid=0 (user is root) or else display a dollar sign ($)

Let’s try this one:

PS1='\u\h\w\$'

The resulting prompt (rootkali~#) is close to the default prompt (root@kali:~#). Let us now insert the hashtag sign (@) sign between the words “root” and “kali” and also insert the colon sign (:) between the word “kali” and the tilde sign (~):

PS1='\u@\h\w:\$'

The resulting shell prompt is now the same as the default prompt, only, without the colors.

As mentioned above the color tag is in the form of:

\[\033[x;ym\] 

where x is an attribute and y is the color code.

Table 1. Text Attributes and Color Codes Table

xy
0 - Normal30 - Black
1 - Bold31 - Red
2 - Dim32 - Green
3 - Italics33 - Yellow
4 - Underline34 - Blue
5 - Blink35 - Purple
6 - NA (Not Applicable, Not used)36 - Cyan
7 - Reverse Foreground and Background Colors37 - White
8 - Hide
9 - Strikeout (Strikethrough)
Text attributes and color codes for bash prompt customization.

Going back to our last shell prompt,

PS1='\u@\h\w:\$'

we’ll add the color tag for red (31) with bold (1) attribute to make the words “root@kali” bold red,

\[\033[1;31m\] 

add the color tag for blue (34) with bold (1) attribute before the tilde sign,

\[\033[1;34m\] 

and finally, insert the color tag for white (37) with normal (0) attribute after the tilde sign,

\[\033[0;37m\]

The resulting code will be:

PS1='\[\033[1;31m\]\u@\h\[\033[1;34m\]\w:\[\033[0;37m\]\$'

Now, we have the Kali’s default shell prompt.

Summary

To summarize, the shell prompt string is stored in the environment variable PS1. The string is composed of escape characters with meanings as defined in the table Bash Prompt Escape Characters. It also contains color tags in the form of,

\[\033[x;ym\]

as defined in the Attribute and Color Codes table.

Dissecting Kali’s Default Prompt

The Kali Linux Terminal’s default shell prompt as quoted at the very start of this post is represented by the string,

\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;33m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$

which is the output of the command:

echo $PS1

The configuration string up to the curly right braced “}” takes effect only when a user changes the apparent root directory by using the command “chroot”.

\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}

Since we are not chroot-ing, we’re interested only on the part of the environment string past the curly right braces “}”, which is:

\[\033[01;33m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$

Based on our discussion above, here is the dissection of the said prompt:

\[Start of color information.
\033[01;33mBold Yellow
\]End of color information.
\uDisplay the current user name
@Display the “@” sign.
\hDisplay the host name.
\[ Start of color information.
\033[00m
\033[00mColor information (See table below).
\]End of color information.
:Display the colon sign(“:”).
\[Start of color information.
\033[01;34mBold Blue
\]End of color information.
\wDisplay the current working directory (tilde sign “~”)
\[Start of color information.
\033[00m
\]End of color information.
\$If user is root, display “#”, else display “$”

Table 2. Bash Prompt Escape Characters

Escape CharacterDescription
\aA bell character.
\dThe 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.
\eAn escape character.
\hThe hostname, up to the first ‘.’.
\HThe hostname.
\jThe number of jobs currently managed by the shell.
\lThe basename of the shell’s terminal device name.
\nA newline.
\rA carriage return.
\sThe name of the shell, the basename of $0 (the portion following the final slash).
\tThe time, in 24-hour HH:MM:SS format.
\TThe time, in 12-hour HH:MM:SS format.
\@The time, in 12-hour am/pm format.
\AThe time, in 24-hour HH:MM format.
\uThe username of the current user.
\vThe version of Bash (e.g., 2.00).
\VThe release of Bash, version + patchlevel (e.g., 2.00.0).
\wThe current working directory, with $HOME abbreviated with a tilde (uses the $PROMPT_DIRTRIM variable).
\WThe basename of $PWD, with $HOME abbreviated with a tilde.
\!The history number of this command.
\#The command number of this command.
\$If the effective uid is 0, #, otherwise $.
\nnnThe character whose ASCII code is the octal value nnn.
\\A backslash.
\[Begin a sequence of non-printing characters. This could be used to embed a terminal control sequence into the prompt.
\]End a sequence of non-printing characters.
Source: https://www.gnu.org/software/bash/manual/bash.pdf
Bash Reference Manual
Reference Documentation for Bash
Edition 5.0, for Bash Version 5.0.
May 2019
Chapter 6.9 - Controlling the Prompt, Pages 97-98
Downloaded 19Sept2019

Related Articles:

How to Partition a Disk in Linux with fdisk

References:

https://www.gnu.org/software/bash/manual/bash.pdf

 

 

Leave a comment

Your email address will not be published. Required fields are marked *