How to Change / Set up bash custom prompt (PS1) in Linux
How to Change / Set up bash custom prompt (PS1) in Linux
Most of us work with a shell prompt. By default most Linux distro displays username@hostname and current working directory. You can easily customize your prompt to display information that make sense to you. You can change look and feel by adding colors.
In this article i will show how to do that:
How To Customize Bash Prompt In Linux
You need to set PS1, PS2, PS3 and PS4 variable.
See the current bash shell prompt setting:
echo $PS1
How do I modify or change the prompt?
Just assign a new value to PS1 and hit enter key:
Old Prompt : root@test-bastion
PS1="bastion1: "
Output:
bastion1:
Now setup prompt to display date/time, hostname and the current working directory:
PS1="[\d \t \u@\h:\w ] $ "
Output:
[Thu Jul 01 17:52:45 root@test-bastion:~ ] $
Set the prompt so that it can display today’s date and hostname:
PS1="\d \h $ "
Output:
Thu Jul 01 test-bastion $
Let's Colur Code it:
Let us say when you login as root, you want to get visual confirmation using red color prompt. As you already Know to distinguish between root and normal user you use last character in the prompt, if it changes from $ to #, you have root privileges.
So let us set your prompt color to RED when you login as root, otherwise display normal prompt.
# vi /etc/bashrc
# If id command returns zero, you got root access.
if [ $(id -u) -eq 0 ];
then # you are root, set red colour prompt
PS1="\\[$(tput setaf 1)\\]\\u@\\h:\\w #\\[$(tput sgr0)\\]"
else # normal
PS1="[\\u@\\h:\\w] $"
fi
Close and save the file.
No comments