Tag Archives: Show hostname in shell prompt

How to change your shell prompt to fancy one instantly

Learn to change shell prompt with your chosen character or value. Different values or shell variables can be defined to be shown as shell prompt.

After the user logged in to the system through Putty or command line he is greeted with blinking cursor in front of something called “Shell prompt”! Generally, the shell prompt with # denotes superuser and with $ denotes normal user. But going beyond these mainstream prompts, most of the admins choose the custom prompt for them and their users.

The most famous prompt used is showing the present working directory in prompt. So that users know in which directory he is while executing any command. Another widely used prompt is showing hostname. This ensures the user that he is working on the 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 the profile file. This profile file can be any profile that is executed on user login. If multiple profiles have multiple values defined for PS1 then the last profile executed will decide the 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 system-wide profile i.e. /etc/profile calls bash profile which resides in the user’s home directory. This local profile calls bashrc script residing in the home directory. This bashrc calls up system-wide /etc/bashrc script to set the 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 the profile file is missing in the 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. Let’s see how to define it. It can be defined in a very basic way as below :

PS1=":->"
export $PS1

Here we are defining prompt as the symbol :-> The export command is not necessary but it’s 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 an if-else loop in the profile file to decide which prompt should be served for particular users or terminal types.

Different useful prompts :

Below is the useful list of variables can be used in prompts :

Code
Description
PS1=”[$USER@$HOSTNAME]$” Shows prompt as [username@hostname]$
PS1=”[$USER@$HOSTNAME $PWD]$” Shows prompt as [username@hostname Present_directory]$
PS1=”[$HOSTNAME $PWD]$” Shows prompt as [hostname present_directory]$
PS1=”$HOSTNAME >” Shows prompt as hostname >

You can choose your own variations. See above-listed prompts in action below :

$PS1="[$USER@$HOSTNAME]$"
[user4@testsrv2]$

$PS1="[$USER@$HOSTNAME $PWD]$"
[user4@testsrv2 /home/user4]$

$PS1="[$HOSTNAME $PWD]$"
[testsrv2 /home/user4]$

$PS1="$HOSTNAME >"
testsrv2 >

Observe the first prompt is just $ sign. After each PS1 value change, prompt changes accordingly.

Some fancy prompts :

Here are some fancy prompts for fun!

$PS1=">-->"
>-->

$PS1="-=(^_^)=-:"
-=(^_^)=-:

$PS1="\m/ (-_-) \m/ :"
\m/ (-_-) \m/ :

$PS1="$USER rules $HOSTNAME >"
user4 rules testsrv2>

Hope you liked the post. Drop us your feedback, suggestions in comments below! Do follow us on Facebook, Twitter & Google+