Howto create custom BASH Shell Prompts

Creating custom PS1 and PS2 command prompts

By default most Shell prompts display information relating to the user and the hostname. For example:

tux@linux-z2jb:~> whoami

From the above we can see that the user "tux" is logged into server "linux-z2jb"

You may often see the command prompt with a dollar sign "$", generally this signifies a none root user. You will also see the prompt "#", where the hash or pound symbol signifies you are using a root account.

Typical Command Prompt Examples

Ubuntu 14.04 LTS Linux Distribution


Notice the "$" and "#" symbols signify the type of account in use. ( # - root and $ - normal user)

openSUSE 13.2 Linux Distribution

linux-z2jb:~ #

Notice only the root account uses the "#" symbol.

Bash Command prompt Shell Variables

The information displayed by your prompt is controlled by various shell variables: PS1, PS2, PS3 and PS4.

To display the current settings of your prompt we can use the "echo" command along with the relevant variable:

tux@linux-z2jb:~> echo $PS1

The primary prompt is PS1, and where more information information is required by the shell the PS2 prompt is used.

Extract Taken from "man bash" pages:

       PS1    The value of this parameter is expanded and used as the primary prompt string.  
       PS2    The value of this parameter is expanded as with PS1 and used  as
              the secondary prompt string.  The default is ``> ''.
       PS3    The value of this parameter is used as the prompt for the select
       PS4    The value of this parameter is expanded  as  with  PS1  and  the
              value  is  printed  before  each command bash displays during an
              execution trace.  The first character of PS4 is replicated  mul-
              tiple  times, as necessary, to indicate multiple levels of indi-
              rection.  The default is ``+ ''.

When executing interactively, Bash displays the primary prompt "PS1" when it is ready to read a command, and the secondary prompt PS2 is displayed when more input is required to complete a command. Bash allows these prompt strings to be customized by inserting a number of "backslash-escaped" special characters that are decoded as follows:

              \a     an ASCII bell character (07)
              \d     the date in "Weekday Month Date" format (e.g.,  "Tue  May
                     the  format  is  passed  to strftime(3) and the result is
                     inserted into the prompt string; an empty format  results
                     in a locale-specific time representation.  The braces are
              \e     an ASCII escape character (033)
              \h     the hostname up to the first `.'
              \H     the hostname
              \j     the number of jobs currently managed by the shell
              \l     the basename of the shell's terminal device name
              \n     newline
              \r     carriage return
              \s     the name of the shell, the basename of  $0  (the  portion
                     following the final slash)
              \t     the current time in 24-hour HH:MM:SS format
              \T     the current time in 12-hour HH:MM:SS format
              \@     the current time in 12-hour am/pm format
              \A     the current time in 24-hour HH:MM format
              \u     the username of the current user
              \v     the version of bash (e.g., 2.00)
              \V     the release of bash, version + patch level (e.g., 2.00.0)
	      \w     the  current  working  directory,  with $HOME abbreviated
                     with a tilde (uses the value of the PROMPT_DIRTRIM  vari-
              \W     the basename of the current working directory, with $HOME
                     abbreviated with a tilde
              \!     the history number of this command
              \#     the command number of this command
              \$     if the effective UID is 0, a #, otherwise a $
              \nnn   the character corresponding to the octal number nnn
              \\     a backslash
              \[     begin a sequence of non-printing characters, which  could
                     be  used  to  embed  a terminal control sequence into the
              \]     end a sequence of non-printing characters

Example of changing your PS1 prompt

Current Setting:

tux@linux-z2jb:~> echo $PS1

The current setting for the primary PS1 prompt is \u@\h:\w> From the above output we can see that "\u" specifies the current user, "\h" specifies the host name upto the first dot".", and the "\w" specifies the current working directory, with $HOME abbreviated with the tilde "~" symbol.

The ":" is used as a simple separator.

If we wanted to change our prompt to display the user, full hostname, current working directory followed by the time we could specify a string similar to the following example:
(simply type PS1='[\u@\H:\w:\A]:\$ ' at your current command prompt)

PS1='[\u@\H:\w:\A]:\$ '


Now we have a custom PS1 prompt. As a quick example, I have created an interactive loop which displays the value of the PS2 variable ">" After we enter our first line of our command:
"for i in $PS1 $PS2" and press enter, the PS2 prompt is displayed as it requires more information to complete the command sequence:

[]:$ for i in $PS1 $PS2
> do
> echo $i
> done

If we wanted to change the PS2 prompt value from ">" to "=>" we could issue the command: PS2='=> '

[]:$ PS2='=> '

If we run our interactive loop from the command line again, we can see the new PS2 value:

[]:$ for i in $PS1 $PS2
=> do
=> echo $i
=> done

If you wanted to change your shell prompts on a more permanent basis, you could modify your shell start-up file : ~/.bashrc by adding the following lines to the bottom of the file:

# My Custom PS1 and PS2 Prompts
# New PS1 and PS2 follows
PS1='[\u@\H:\w:\A]:\$ '
PS2='=> '

Now when you start a shell you will have your new PS1 and PS2 prompts!