Learn to change shell prompt with your choice character or value. Different values or shell variables can be defined to be shown as shell prompt.
After user logged in to system through Putty or command line he is greeted with blinking cursor in front of something called as “Shell prompt”! Generally, shell prompt with # denotes superuser and with $ denotes normal user. But going beyond this mainstream prompts, most of the admins choose custom prompt for them and their users.
Most famous prompt used is showing present working directory in prompt. So that users knows in which directory he is while executing any command. Another widely used prompt is showing hostname. This ensures user that he is working on right terminal when many terminal windows are open. In this post we will see how to set these prompts and some fancy prompts too.
Where to define Shell prompt :
Shell prompt is defined by PS1 variable in profile file. This profile file can be any profile which is executed on user login. If multiple profiles have multiple values defined for PS1 then the last profile executed will decide final value for PS1. For example when user logs in below profile execution can be followed :
/etc/profile -> ~/.bash_profile -> ~/.bashrc -> /etc/bashrc
In above flow systemwide profiel i.e. /etc/profile calls bash profile which resides in user’s home directory. This local profile calls bashrc script residing in home directory. This bashrc calls up system wide /etc/bashrc script to set environment. In this case PS1 value defined in /etc/bashrc would be the final one.
Sometimes there were no scripts called from profile then user’s home directory profile would be last resort to define PS1. If profile file is missing in user’s home directory the PS1 defined in /etc/profile will decide how your prompt looks.
How to define shell prompt :
Now, you know the file where prompt can be defined. Lets see how to define it. It can be defined in very basic way as below :
Here we are defining prompt as symbol :-> Export command is not necessary but its good to have it in some flavors of Linux or Unix. Even you can test it by running command PS1=”:->” on your terminal and you can see immediately your prompt will be changed to :->
You can even use if-else loop in profile file to decide which prompt should be served for particular users or terminal types.
Different useful prompts :
Below is useful list of variables can be used in prompts :
You can choose your own variations. See above listed prompts in action below :
Observe first prompt is just $ sign. After each PS1 value change, prompt changes accordingly.
Some fancy prompts :
Here are some fancy prompts for fun!
$PS1="\m/ (-_-) \m/ :"
\m/ (-_-) \m/ :
$PS1="$USER rules $HOSTNAME >"
user4 rules testsrv2>