How we can use our environment variable to customize our command prompt

PS stand for the prompt statement. There are four PS such as PS1, PS2, PS3 and PS4.

Use the command below to display PS1 variable value:

set | grep ps

GROUPS=()
PS1='[\[email protected]\h \W]\$ ‘
PS2=’> ‘
PS4=’+ ‘

PS1='[\[email protected]\h \W]\$ ‘
This display information about username, hostname and end path of current directory.

Where,
\u = root or username
\h = hostname
\W = end part of current working directory

PROMPTING

You can check all bash prompt strings by using the following command

man ps

 

Examples:

1.) Display username, hostname and full current working directory path in the command prompt

export PS1="[\[email protected]\h \w]\$ "

2.) To add color to the shell prompt. We can use the following export command:

export PS1="\e[x;ym [\[email protected]\h \W]\$ \e[m"

$ export PS1=”\e[0;34m[\[email protected]\h \W]\$ \e[m ”

Where,

\e[ : Start color scheme.
x1;y1: Means color pair which we want to use (x1;y1)
$PS1: Your shell prompt variable.
\e[m : Stop color scheme.

Some color codes:

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

The color change can be made permanent by adding the following lines to .bash_profile or .bashrc

SCOLOR='\e[0;34m';
ECOLOR="\e[0m"
export PS1="$SCOLOR[\[email protected]\h \W]\$ $ECOLOR"

or

export PS1="\e[0;34m[\[email protected]\h \W]\$ \e[m"

Save and close the file.

3.) Display current time in the prompt

You can use \t to display the current time in the hh:mm:ss format as shown below:

export PS1="\[email protected]\h [\t]> "

[email protected] [07:15:32]>

Recommend Manual command

$ man bash

If you have any question, leave your comment or If you’d like to share your prompt, please do so in the comments below.

Add Comment