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
1 | \[\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
1 | echo $PS1 |
Output
1 | \[\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.
1 2 3 4 5 | PS1=$ PS1=# PS1=Jean PS1="Jean Valjean" PS1=24601 |
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,
1 | root@kali~# |
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 Code | Meaning |
\u | Username |
\h | Hostname |
\w | Working directory |
\$ | Display a hashtag if uid=0 (user is root) or else display a dollar sign ($) |
Let’s try this one:
1 | 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 (~):
1 | 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:
1 | \[\033[x;ym\] |
where x is an attribute and y is the color code.
1 | [table id=14 /] |
Going back to our last shell prompt,
1 | 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,
1 | \[\033[1;31m\] |
add the color tag for blue (34) with bold (1) attribute before the tilde sign,
1 | \[\033[1;34m\] |
and finally, insert the color tag for white (37) with normal (0) attribute after the tilde sign,
1 | \[\033[0;37m\] |
The resulting code will be:
1 | 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,
1 | \[\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,
1 | \[\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:
1 | 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”.
1 | \[\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:
1 | \[\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;33m | Bold Yellow |
\] | End of color information. |
\u | Display the current user name |
@ | Display the “@” sign. |
\h | Display the host name. |
\[ | Start of color information. |
\033[00m | |
\033[00m | Color information (See table below). |
\] | End of color information. |
: | Display the colon sign(“:”). |
\[ | Start of color information. |
\033[01;34m | Bold Blue |
\] | End of color information. |
\w | Display 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 Character | Description |
---|---|
\a | A bell character. |
\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 escape character. |
\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 | A newline. |
\r | A carriage return. |
\s | The name of the shell, the basename of $0 (the portion following the final slash). |
\t | The time, in 24-hour HH:MM:SS format. |
\T | The time, in 12-hour HH:MM:SS format. |
\@ | The time, in 12-hour am/pm format. |
\A | The 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 + patchlevel (e.g., 2.00.0). |
\w | The current working directory, with $HOME abbreviated with a tilde (uses the $PROMPT_DIRTRIM variable). |
\W | The 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 $. |
\nnn | The 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. |
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