If you’re like me you always intend to customize your UNIX or Linux prompt but never seem to find the time to look up the options and make the change. Well, to give you a jump start here’s an excerpt from my book Easy Linux Commands.
In order to eliminate the need to frequently issue the pwd command to determine the current working directory, many Linux users choose to display the working directory within the Linux command prompt. Some Linux administrators even provide this service for their users when they create their Linux accounts. If a user’s command prompt does not contain the working directory, the command prompt can be modified by changing the prompt string 1 (PS1
) shell variable as demonstrated here:
$ PS1="[\u@\h \w]\\$ "
[tclark@appsvr /home/tclark]$
This example of setting the PS1
variable also adds the username (\u
) and hostname (\h
) to the prompt. This can be very useful if you frequently connect to different hosts and as different users.
In order to avoid having to modify the prompt at each login, the following line of code can be placed within the appropriate configuration file, such as .bash_profile for the bash shell, within the home directory. To do this you can use a text editor like vi to add the following line to your .bash_profile
file:
export PS1="[\u@\h \w]\\$ "
Note: Files that begin with a period will not appear when you list the contents of a directory. To see these hidden files use the command ls –a
There are even more options which you can put into your PS1
prompt. While it’s nice to keep your prompt fairly short you may find some of the other options useful. The following table contains a list of values that can be displayed within the PS1
and/or PS2
prompt strings:
Symbol |
Displayed Value |
\! |
History number of current command |
\# |
Command number of current command |
\d |
Current date |
\h |
Host name |
\n |
Newline |
\s |
Shell name |
\t |
Current time |
\u |
User name |
\W |
Current working directory |
\w |
Current working directory (full path) |
As a bonus, here are a few of my favorite options for the PS1
prompt:
Prompt:
export PS1='\u@\h$ '
Result:
oracle@glonk$
Prompt:
export PS1='\w$ '
Result:
/usr/local$
Prompt:
export PS1='\d$ '
Result:
Mon Oct 30$
You may notice that all these have the dollar sign ($) in them which is typical of bash prompts. There is also a space after the dollar sign so you can easily tell where your prompt ends and your commands begin.

Buy it now!
I find that deeply nested directory structures, typical of applications and database installations, cause the single-line prompt leave little room on the cmdline and the varying location of the command entry location forces a focus change on every directory change.
I configure a 2 line prompt, the first line is the cwd in an alternate display mode (usually reverse), the second is “nodename:username >” with the domain stripped off of the FQDN.
For bash the prompt definition is:
PS1=’\e[7m\w\e[0m\n\h:\u >’
The alternate display mode of the first line serves as a quick visual reference to mark the location of the last command entered.
The csh, tcsh, sh and ksh shell prompts can be similarly customized, see the man pages for specifics.
As a DBA I find the following variation useful:
PS1=”
\u \h \${ORACLE_SID} \d \${PWD}
\${DT} \t > ”
Gary
I don’t need to buy the book, you’re giving it to me for free! 🙂
Alan,
Moving the working directory onto its own line is a good move. I personally exclude it because it tends to get long.
For those of you following at home, Alan’s prompt would look about like this:
/usr/local
sprog:oracle >
except the /usr/local part would be in white-on-black text (or the reverse of whatever your terminal is.
Nice one Alan!
Gary,
Another good one, very thorough.
Gary’s prompt would look like this:
oracle sprog oss Tue Oct 31 /usr/local
17:07:00 >
You can’t really see it here, but there is a blank line at the beginning of it which will separate it from the previous output. oracle is the username, sprog the hostname, oss the ORACLE_SID variable.
Thanks for sharing!
Robert V said:
–I don’t need to buy the book, you’re giving it to me for free! 🙂
I’m trying to Robert! Of course there is plenty in the book that I haven’t/won’t post online, but either way it’s all about sharing the knowledge. Still, it’s nice to have a book on the shelf that’s well organized, not prone to network outages and the same information is always on the same page number.
Me, I’m very happy to be providing information both ways. 🙂
This information is so 10 years ago… 🙂
Although google seems to like you.
http://www.google.com/search?hl=en&q=customize+linux+prompt&btnG=Google+Search
You’re a weenie, Camden.
Thank you God, I dind’t want to be the one to say it.
Hi there !
I stumbled upon this site and thought I could give Gary’s promt a try. I like it a lot and modified it a bit for use with colorful terminals. Here’s what I use now:
PS1="\[33[01;32m\]\u@\h \[33[01;31m\]uid:\${UID} \[33[01;34m\]\d \[33[01;33m\]\w
\[33[00m\]\t \[33[01;32m\]>\[33[00m\] "
The uid display is arguable I guess and I use it only on the root prompt.
Cheers !
Thanks for the tips!
Very informative and simple to implement….good work with dedication. thx.
Very Informative.
Hi,
Could you please let me know how can i set my present working view name as my prompt in bash shell?
thanks in advance
Chandra,
Sorry for the delayed response. You should be able to add `pwd` to your prompt to display the current directory. Note that it needs to be between back-quotes (shift-tilde).
Hope this helps.
I’m searching for a way to display the version of the system and for the prompt to change colors when I am running commands as root.
Any ideas?
Thanks
on the colorful prompt note, i was inspired by the linux from scratch live cd prompt. so i changed it up a little:
for normal user:
PS1=’\[\e[1;32m\]\u [ \[\e[1;33m\]\h\[\e[1;32m\] ][ \[\e[1;33m\]\w\[\e[1;32m\] ]$ \[\e[0m\]’
for root:
PS1=’\[\e[1;31m\]\u [ \[\e[1;33m\]\h\[\e[1;31m\] ][ \[\e[1;33m\]\w\[\e[1;31m\] ]# \[\e[0m\]’
basically, its like so:
coredump [ server ][ ~ ]$
for normal user, its all green except the host name and the pwd is yellow. for root, the green is red. i havent had to deal too much with long directory names, if it gets to be a prob, i will move the [ ~ ] above the prompt:
[~]
coredump [ server ]$
i like the host name in there because i deal with several linux boxes and am always cross shelling into them from the others and it helps to keep some sanity.
This works for the first login for me. For root login after (i.e. su -), I needed to do the PS1 and export in two separate steps:
###
PS1=”[\u@\w]\\$ ”
export PS1
###
Environment:
###
[root@/var/www/html]# lsb_release -a
LSB Version: :core-3.1-ia32:core-3.1-noarch:graphics-3.1-ia32:graphics-3.1-noarch
Distributor ID: CentOS
Description: CentOS release 5.4 (Final)
Release: 5.4
Codename: Final
[root@/var/www/html]# /bin/bash –version
GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
###
cheers
Jabez Networks, Inc.
Best in SEO and China Business
@coredump, thanks, this prompt is really nice looking!
@Jon E, thanks for the great reference!
Great tips, thanks.
Jon — thanks very much for this post, it was exactly what I was looking for. Thanks to all the helpful commenters as well!
Thanks Guys ! Just what I was looking for !