Customize your bash prompt
Categories: Database Administration, Information Technology, System Administration, UNIXIf 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.
For more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.Buy it now!

October 31st, 2006 at 10:56 am
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.
October 31st, 2006 at 4:51 pm
As a DBA I find the following variation useful:
PS1=”
\u \h \${ORACLE_SID} \d \${PWD}
\${DT} \t > ”
Gary
October 31st, 2006 at 5:04 pm
I don’t need to buy the book, you’re giving it to me for free!
October 31st, 2006 at 5:05 pm
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/localsprog:oracle >
except the /usr/local part would be in white-on-black text (or the reverse of whatever your terminal is.
Nice one Alan!
October 31st, 2006 at 5:12 pm
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!
October 31st, 2006 at 8:31 pm
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.
November 7th, 2006 at 6:51 pm
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
December 2nd, 2006 at 3:40 pm
You’re a weenie, Camden.
December 2nd, 2006 at 3:43 pm
Thank you God, I dind’t want to be the one to say it.
May 31st, 2007 at 12:16 pm
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 !
July 26th, 2007 at 8:02 pm
Thanks for the tips!
October 11th, 2007 at 9:14 am
Very informative and simple to implement….good work with dedication. thx.
October 11th, 2007 at 9:15 am
Very Informative.
December 23rd, 2007 at 8:04 am
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
January 18th, 2008 at 5:50 pm
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.
February 10th, 2008 at 2:24 am
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