Can we get some examples for $PS1 variable?



Can you please provide some examples for $PS1 variable?



PS1 stands for “Prompt String One”. PS1 allows us to control the appearance of the bash default command prompt (The prompt which appears when we login to a Linux box)

Below is the screenshot of the prompt of my web console.

It displays information like

abhinav9884 - My Username in the Linux box
@ - character
ip-172-31-38-183 - Linux box hostname
~ - My current working directory

Let’s see what value PS1 variable contains in my console.

It has the value of [\u@\h \W]$. Let’s see what these characters mean.

u - the username of the current user
h - the hostname up to the first dot
W - the basename of the current working directory

Let’s say we want to see the name of the shell and time also in the command prompt. Type below command in the prompt.

PS1='[\u@\h \W \s \@]\$'

Here ‘s’ denotes the name of the shell and @ will print the current time in 12-hour am/pm format.

See below screenshot with the new prompt

You can also add PS1=’[\u@\h \W \s @]$’ in ~/.bashrc file if you want the same prompt during your future logins.

Hope this helps.

PS - Find the complete list of prompt variable characters here