As said in our the previous post(Linux booting process) about our up coming posts on Linux prompts. This is first in the series which will give you detail information about PS1 prompt.

What is PS1 prompt?

PS1 (Prompt String 1) is one of the prompts available in Linux/Unix. When you try to login to any machine, you have to enter user name and password. Once you are done with this you are presented with some info like who logged in, on what machine he logged in, what is his present working directory and if the logged in user is a super user or a normal user. This is done by using PS1 prompt which is a inbuilt shell variable. The other prompts are PS2, PS3 and PS4.

Example prompt when we login to a machine:

	[[email protected] common]$

If you see above prompt it actually say that "surendra" is the user who logged in to a machine whose name is "linuxnix" to the present working directory "common" and he is a normal user($)

How about superuser login prompt?

	[[email protected] dev]#

If you see above prompt it actually say that "root" is the user who logged in to a machine whose name is "linuxnix" to the present working directory "dev" and he is a superuser user(#). So this prompt changes depending on the user name, his working directory and his privileged access. We can customize this prompt by using some control commands and Shell inbuilt variables.

Control commands available for PS1 Prompt:

	d - the date in "Weekday Month Date" format (e.g., "Tue May 26")
e - an ASCII escape character (033)
h - the hostname up to the first .
H - the full hostname
j - the number of jobs currently run in background
l - the basename of the shells 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., 4.00)
V - the release of bash, version + patch level (e.g., 4.00.0)
w - Complete path of current working directory
W - the basename of the current working directory
! - 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 prompt
] - end a sequence of non-printing characters

We can use above options/commands to make PS1 prompt more meaning full.

Check what is your default PS1 prompt by executing below command:

	echo $PS1
[[email protected] W]$

If you see u will give you user name who logged in to the machine, h will give you the hostname of that machine and W will give you relative path where user is working and $ will give you user previlize details.

Know more about PS1 prompt with examples

Example1: We can change this prompt to different one with a bit meaning such as giving present time of the machine etc as shown in below example.

	PS1='A [[email protected] W]$ '

Prompt changes to:

	07:16 [surendra@linuxnix- scripts]$

If you see I set my PS1 prompt to use time(24 hour format) by defining A option. You can use above mention codes to make your prompt more informative.

Example2: Want to display a command output in your prompt? We can do that using command substitution concept. Suppose I want to display 'uname -r' output along the prompt, I can do that by setting PS1 as shown below.

	PS1='$(uname -r) [[email protected] W]$ '

Prompt changes to:

	2.6.18-194.17.1.el5 [[email protected] scripts]$

Example3: How about using system inbuilt variables in the prompt? We can use that as well with PS1 prompt. Display number of seconds completed when the terminal opened

	PS1='$SECONDS [[email protected] W]$ '

Prompt changes to:

	11450 [[email protected] scripts]$'

Example4: We can even use functions in the PS1 prompt. Suppose you want to keep a check on RAM availability. Define below function in your ~/.bashrc file and use it in PS1 prompt

Function to find free RAM available:

	aram () { free -m | awk '{print $4}' | head -2 | tail -1; }

Using above function in PS1 prompt:

	PS1='$(aram)Mb [[email protected] W]$ '

Prompt changes to:

	119Mb [[email protected] scripts]$

And you can do many more with this prompt. I will leave this to users to explore more on this and enjoy. Other prompts such as  PS2, PS3 and PS4 are posted at our Scripting portal as they are much usefull in Shell scripting rather then to administration. Please have a look at them as well for more information on them.