How to change bash prompt color and look in Linux: PS1, PS2, PS3, PS4 Environment Variables.

Prompt Statement or PS defines various variables to change the look or behavior of the Shell prompt.

There are four prompt statements available alongwiith a prompt command variable in Linux bash shell to customize the prompt behavior.

1. Prompt Statements’ Variables
a. PS1
b. PS2
c. PS3
d. PS4
e. PROMPT_COMMAND

Lets have a look at each of these prompt statements one by one.

a. PS1 – Default Shell Prompt
The default value of PS1 variable for root user is “\s-\v\$”, where \s displays the shell name and \v displays the version number.

However on NGELinux, the default value is modified to be \u@\h \W, where:
\u –> User Name
\h –> hostname
\W –> Present working directory

### PS1 value
[root@nglinux ~]# echo $PS1
[\u@\h \W]\$

### Change the PWD and see the effect.
[root@nglinux ~]# cd Desktop/
[root@nglinux Desktop]# 

b. PS2 – Continuation interactive prompt
We know that in Linux, a very long command can be broken down into multiple lines by giving \ at the end of each line.
“>” is the default interactive prompt in case of a multi-line command.

### default interactive prompt
[root@nglinux Desktop]# a \
> b \
> ^C
[root@nglinux Desktop]# 

### Modify PS2 value and see the effect.
[root@nglinux Desktop]# PS2="next-->"
[root@nglinux Desktop]# ls \
next-->a \
next-->^C

c. PS3 – Prompt displayed by “select” command inside shell script

### Lets create a shell script with select statement
[root@nglinux Desktop]# cat abc.sh 
echo "select age"; 
select i in '<18' '>18'
do 
case $i in 
	'<18') echo "No vote";; '>18') echo "Yes vote"; 
esac; 
done

### Lets execute this script.
### We can see the default prompt displayed is #?
[root@nglinux Desktop]# ./abc.sh 
select age
1) <18 2) >18
#? 1
No vote
#? 2
Yes vote
#? ^C[root@nglinux Desktop]# 

### Now lets define PS3 value and run the script.
[root@nglinux Desktop]# export PS3="Select from the options above>"
[root@nglinux Desktop]# ./abc.sh 
select age
1) <18 2) >18
Select from the options above>1
No vote
Select from the options above>2
Yes vote
Select from the options above>^C[root@nglinux Desktop]# 

d. PS4 – Used to change shell script trace prompt which is displayed when using “set -x”.

### The default trace prompt is "+"
### Lets define to a new value and run the above script.
[root@nglinux ~]# export PS4='****'
[root@nglinux ~]# set -x; ./abc.sh 
****./abc.sh
-bash: ./abc.sh: No such file or directory
*****printf '\033]0;%s@%s:%s\007' root nglinux '~'
[root@nglinux ~]# ./Desktop/abc.sh 
****./Desktop/abc.sh
select age
1) <18 2) >18
#? 1

e. PROMPT_COMMAND :- PROMPT_COMMAND is executed just before displaying the PS1 variable value.

### default prompt command value.
[root@nglinux ~]# echo $PROMPT_COMMAND 
printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"
[root@nglinux ~]# 

### Lets change its value
[root@nglinux ~]# export PROMPT_COMMAND="$PROMPT_COMMAND; echo 'Welcome to NGELinux>'  "
Welcome to NGELinux>
[root@nglinux ~]# 
Welcome to NGELinux>
[root@nglinux ~]# 
Welcome to NGELinux>

2. Changing color of bash prompt
We can add colors to the shell prompt by using below export command syntax:
‘\e[X;Ym $PS1 \e[m’

Where,
\e[ : Start color scheme.
X;Y : Color pair value (X;Y)
$PS1 : Shell prompt variable which we want to color.
\e[m : Stop color scheme.

### Lets have a look at the command syntax and usage.
ngelinux.com ps1 variable



		
		
	

Leave a Reply

Your email address will not be published.