During the week I confused the hostname of a friend's machine to be his username. He has a MacBook Pro and the shell prompt in OS X displays like:
My openSUSE laptop has the following prompt and I like it this way:
He then asked me how to customize the shell prompt. Now, the shell prompt consists of a set of characters that appear every time the shell is ready to accept a command; like we see above. In order to customize the shell prompt with information that we want to display we need to provide some special characters to the
PS1 variable (PS stands for 'prompt string'). Additional input can be provided through PS2, PS3 etc but that's outside the scope of this post.
We can set the prompt temporarily trying various combination of special characters by executing something like
export PS1="[\t \w] $ " to see a prompt like
[14:52:48 ~] $.
As we can see above different sets of special characters can be used to customize the shell prompt accordingly. The table below shows various special characters that can be used to further customize the shell prompt.
|\t||Prints the current time in hours, minutes and seconds.|
|\@||Prints the current time in 12-hour am/pm format.|
|\$||Displays the user prompt ($) or root prompt (#), depending on which user you are.|
|\h||Prints the hostname of the computer running the shell.|
|\H||Prints the full hostname (e.g localhost.localdomain).|
|\u||Prints the current username.|
|\w||Displays the full path of the current working directory|
|\W||Displays only the current working directory base name (e.g /var/log/nginx will be shown as 'nginx' only).|
To make the shell prompt customization permanent, the value of PS1 can be added to the
.bashrc file in the user's home directory (e.g /home/username/.bashrc).