The shell prompt is the string of characters displayed at the start of each command line in a terminal window to indicate that the shell is ready to receive a command.
The shell prompt typically contains some more less useful information and ends with a dollar sign ("$"). It turns out you can fully customize this shell prompt. As part of the prompt you can display for example, the current user account, the computer/host name, time, date, the current directory (folder), or the full path the current directory. You can even write and specify a script that computes any kind of information would be like to shown as part of the prompt. Additionally, you can set the font style, and font color, as well as background color of the prompt.
The environment variable that specifies how the prompt appears is PS1, which stands for "Prompt Statement 1". Besides the primary prompt (PS1) there are a few others that we will discuss later.
Here is a simple example of how to set the text displayed as prompt:
export PS1="Now what? "
The export command sets the PS1 variable to the string specified to the right of the equal sign and makes it accessible as an environment variable.
You can add the current directory as follows:
export PS1="In \W - now what? "
Add the full path of the directory with the \w parameter:
export PS1="In \w - now what? "
The computer (host) name can be accessed using the \h parameter:
export PS1="On \h in \w - now what? "
And the user name can be accessed with \u. This is illustrated in the following command line:
export PS1="\u@\h \w$ "
The following line shows how to change the font color of the command prompt:
export PS1="\e[0;31m(\u@\h \W)> \e[m"
The character sequence "\e[" marks the start of the a color scheme. The characters "0;31" specify font and font color, the "m" marks the end of the color scheme specification. The character sequence "\e[m" terminates the color scheme.
The number "31" in the above example is the code for the color "red". You can select other colors using one of the following codes:
In order for the PS1 variable to be automatically set for all terminals (shells) being opened, you would have to the add the export command line to the .bash_rc or .bash_profile files.
If you replace the "0" with a "1" the prompt character will appear in bold, as in this example:
export PS1="\e[1;32m(\u@\h \W)> \e[m
A "2" will make it dim and a "4" will give it underscores.
If you replace 30, 31, 32, etc. with 90, 91, 92, etc. the font color will in intensified.
You can specify background colors using the codes 40, 41, 42, etc. The following example will create a yellow background:
export PS1="\e[1;43m(\u@\h \W)> \e[m
High intensity backgrounds can be generated with the codes 100, 101, 102, etc.
The following example sets the prompt font to red on yellow background:
export PS1="\e[1;31;103m(\u@\h \W)> \e[m"
Besides the PS1 variable of the primary shell prompt, you can set the continuation prompt with the PS2 variable. The continuation prompt is relevant when a command line is broken down into multiple lines, typically for readability purposes.
As you may know, you can continue a command line on the next line using the backslash character before hitting the return key to prevent the shell from trying to execute the current line.
The default continuation prompt is ">". You can change this continuation prompt by setting (exporting) the PS2 environment variable, as in this example:
export PS2=" continued > "
The PS3 variable is used to set the prompt used by the select function in a bash shell script.
The PS4 variable defines the prompt used when tracing a shell script using the set command with "-x" option.
Finally, the PROMPT_COMMAND variable can be used to execute any command or script to produce a string that will be displayed before the contents of PS1 displayed. The PROMPT_COMMAND is set as in this example:
export PROMPT_COMMAND="echo -n [$(date)]"