Tuesday 3 June 2014

Prompt Strings - A Best Friend Forever

Hi I am a prompt string. I live in Deva's (My Master) System. It is me that you see as a prompt in the shell. My work is to show useful information that the master requires without distracting him.

I am completely configurable right from what information I give to what color I appear.  But most people tend to underestimate my capabilities and leave me naked. So I came to speak for myself.

Here is how I look in my master's system.

\e[0;31m\h-\u (\w)\$\e[m

I know, I may appear a little intimidating. But worry not, I am going to dismantle and describe myself. Before that, let me explain what I am capable of. Here are some of the stuffs I can show.


  • Date/Time in different formats
  • Host Name
  • User Name
  • Name of the shell and its Version
  • Current working directory ($HOME is denoted by ~)
  • Number of jobs currently managed by the shell
  • Git information
  • Results of custom commands


This is not an exhaustive list. My capabilities are limited by your imagination. Enough chit chat, lets discuss about my building blocks. The Shell do not recognise me by the name prompt string. They call me PS1. My siblings are identified by PS2, PS3 and PS4. I'll talk about them later.

Here I will take different forms and then explain you what makes me look what I am.

\h shows the hostname of the system (beast)
the hyphen (-) is just a static text shown as it is
\u shows the current username (devarajn)
\$ shows a # if the effective UID is 0, otherwise shows $






\d shows the current date in the default format
\w shows the current working directory (~ denotes the home directory)
the space in between is retained in the prompt as well
the dollar symbol $ is a static text as well. (This is different from \$)

you can also play with cool characters such as  ☠ ✂ ☆ ✈ 
But be sure to enclose them in \[ <non printable character> \]

Lets have some fun with colors,






\e[0;31m \e[m gives me red color
\d \w$ in between is same as before.

0;31 can be replaced by other color codes such as 0;30 0;32 0;33 0;34 0;35 0;36. You can replace the 0 with 1 to get the lighters versions of the colors.

Colors can also be set using tput. You can find a detailed list of building blocks here

As you know, I am PS1, the primary prompt string. I have a few siblings

PS2 - Continuation Prompt String, used when the command extends to multiple lines. Default is >

PS3 - Used as a prompt for the select command

PS4 - Prompt string that is used when you execute a shell script is debug mode using set -x. Default is +

Tools may come and go, languages may become obsolete, Technologies may become forefathers. But I shall sail with you throughout your journey. Don't take me naked.

Best Friend Forever
PS1