Customize your bash prompt

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:


Displayed Value


History number of current command


Command number of current command


Current date


Host name




Shell name


Current time


User name


Current working directory


Current working directory (full path)

As a bonus, here are a few of my favorite options for the PS1 prompt:

export PS1='\u@\h$ '

export PS1='\w$ '

export PS1='\d$ '
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.

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!

linux, unix, system administration, sysadmin, bash, shell, unix shell, shell prompt

22 thoughts on “Customize your bash prompt”

  1. 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.

  2. As a DBA I find the following variation useful:

    \u \h \${ORACLE_SID} \d \${PWD}
    \${DT} \t > ”


  3. 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:

    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!

  4. 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!

  5. 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. 🙂

  6. 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 !

  7. 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

  8. 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.

  9. 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?


  10. 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.

  11. 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

    [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.


    Jabez Networks, Inc.
    Best in SEO and China Business

  12. Jon — thanks very much for this post, it was exactly what I was looking for. Thanks to all the helpful commenters as well!

Leave a Reply

Your email address will not be published. Required fields are marked *