MakeTechEasier
  • Home
  •  
  • About
  •  
  • Archives
  •  
  • Jobs
  •  
  • Advertise
  •  
  • Contact Us
  • Windows
  • Linux
  • Mac
  • Internet
  • WordPress
  • iPhone
  • Android
  • All Categories
    • Android RSS
    • Firefox RSS
    • Gaming RSS
    • Google RSS
    • google chrome RSS
    • Internet RSS
    • iPhone/iPod Touch RSS
    • Linux RSS
    • Mac RSS
    • Mobile RSS
    • OpenOffice RSS
    • PC Hardware RSS
    • Review RSS
    • Roundup RSS
    • Windows RSS
    • Windows Mobile RSS
    • WordPress RSS
    • WordPress Plugins RSS
  • rss icon
  • email icon
  • facebook icon
  • twitter icon

8 Useful and Interesting Bash Prompts

Posted by: Joshua Price on September 4th, 2009
  • Comments
  • Share

bachprompts-mainMany people don’t think of their command prompt as a particularly useful thing, or even pay it much attention. To me, this is a bit of a shame, as a useful prompt can change the way you use the command line. Well I’ve scoured the Interwebs looking for the best, most useful, or sometimes most amusing bash prompts. Here, in no particular order, are the ones I’d be most likely to use on my computers.

Note – to use any of these prompts, you can copy & paste the “PS1=” line directly into your terminal. To make the change permanent, paste the line to the end of your ~/.bashrc file.

1. Show Happy face upon successful execution

This prompt is probably the most amusing one on the list, but remains useful. The idea is that as long as your commands execute successfully, your prompt shows a happy face. Any time a command fails, it shows a sad face instead.

Example:
bashprompts-happyface
Code:

PS1="\`if [ \$? = 0 ]; then echo \[\e[33m\]^_^\[\e[0m\]; else echo \[\e[31m\]O_O\[\e[0m\]; fi\`[\u@\h:\w]\\$ "

2.Change color on bad command

Here’s one of my favorites. This prompt has it all. Like above, the prompt changes color if your last command failed to run successfully, but it also shortens long paths and contains the bash history number of each command for easy retrieval.

Example:
bashprompts-hurring
Code:

PROMPT_COMMAND='PS1="\[\033[0;33m\][\!]\`if [[ \$? = "0" ]]; then echo "\\[\\033[32m\\]"; else echo "\\[\\033[31m\\]"; fi\`[\u.\h: \`if [[ `pwd|wc -c|tr -d " "` > 18 ]]; then echo "\\W"; else echo "\\w"; fi\`]\$\[\033[0m\] "; echo -ne "\033]0;`hostname -s`:`pwd`\007"'

3. Multi lines prompt

If you’re the type who wants to pack your prompt full of information, then here’s the one for you. This one is a multi-line prompt containing date/time, full path, user and host, active terminal, even file count and space usage.
Example:
bashprompts-informant
Code:

PS1="\n\[\033[35m\]\$(/bin/date)\n\[\033[32m\]\w\n\[\033[1;31m\]\u@\h: \[\033[1;34m\]\$(/usr/bin/tty | /bin/sed -e 's:/dev/::'): \[\033[1;36m\]\$(/bin/ls -1 | /usr/bin/wc -l | /bin/sed 's: ::g') files \[\033[1;33m\]\$(/bin/ls -lah | /bin/grep -m 1 total | /bin/sed 's/total //')b\[\033[0m\] -> \[\033[0m\]"

4. Color manage your prompt

There’s nothing particularly fancy about this prompt, other than the good use of color to separate the different pieces of information. As you can see, it provides time, username, hostname, and current directory. Fairly minimal but useful.

Example:
bashprompts-4
Code:

PS1="\[\033[35m\]\t\[\033[m\]-\[\033[36m\]\u\[\033[m\]@\[\033[32m\]\h:\[\033[33;1m\]\w\[\033[m\]\$ "

5. Show full path

This one’s a nice, clean, minimal 2-line prompt (plus a blank line at the top). You’ve got your full path at the first line and pretty much just the username at the bottom. If you want to remove the blank line at the start of every prompt, just take out the first “\n”.

Example:
bashprompts-5

Code:

PS1="[\[\033[32m\]\w]\[\033[0m\]\n\[\033[1;36m\]\u\[\033[1;33m\]-> \[\033[0m\]"

6. Show background job count

Another nifty 2-liner, but this one’s got some info we haven’t used before. The first line is the normal user@host, with full path. On the second line we’ve got history number and a count of the jobs running in the background.

Example:
bashprompts-6

Code:

PS1='\[\e[1;32m\]\u@\H:\[\e[m\] \[\e[1;37m\]\w\[\e[m\]\n\[\e[1;33m\]hist:\! \[\e[0;33m\] \[\e[1;31m\]jobs:\j \$\[\e[m\] '

7. Display directory information

A very elegant and nice looking prompt design. With this one we’ve got user/host, number of jobs, and date/time on the top line. Below that is current directory along with number of files in that directory and their disk usage.

Example:
bashprompts-7

Code:

PS1="\n\[\e[30;1m\]\[\016\]l\[\017\](\[\e[34;1m\]\u@\h\[\e[30;1m\])-(\[\e[34;1m\]\j\[\e[30;1m\])-(\[\e[34;1m\]\@ \d\[\e[30;1m\])->\[\e[30;1m\]\n\[\016\]m\[\017\]-(\[\[\e[32;1m\]\w\[\e[30;1m\])-(\[\e[32;1m\]\$(/bin/ls -1 | /usr/bin/wc -l | /bin/sed 's: ::g') files, \$(/bin/ls -lah | /bin/grep -m 1 total | /bin/sed 's/total //')b\[\e[30;1m\])--> \[\e[0m\]"

8. My Prompt

And finally, the prompt I personally like to use. It’s a modification of #7, changed to take up less space and include only the information I most want in my prompt. I like the two-line style as it lets me see the full path without reducing the space for my actual commands.

Example:
bashprompts-8
Code:

PS1="\n\[\e[32;1m\](\[\e[37;1m\]\u\[\e[32;1m\])-(\[\e[37;1m\]jobs:\j\[\e[32;1m\])-(\[\e[37;1m\]\w\[\e[32;1m\])\n(\[\[\e[37;1m\]! \!\[\e[32;1m\])-> \[\e[0m\]"

If you’d like to share your prompt, please do so in the comments below.


Josh Price is a Linux professional and contributing writer to MakeTechEasier.com

Tags: bash, Linux, prompt, shell
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

Share and Enjoy!

  • StumbleUpon It!
  • reddit

Related Posts

  • Making The Linux Command Line A Little Friendlier
  • Transform Kubuntu Jaunty to Windows 7 In 3 Simple Steps
  • How to Install Software from a Tarball in Linux
  • Backing Up Data In Ubuntu Using sBackup
  • How to Run Multiple Linux Distros Without Virtualization

Popular Posts

  • Turn Your Ubuntu Hardy to Mac OSX Leopard 502 comment(s)
  • Turn Your Ubuntu Intrepid Into Mac OSX Leopard 315 comment(s)
  • Download EASEUS Partition Master For Free – 24 Hours Only 175 comment(s)
  • How To Boot And Install Windows 7 From USB Flash Drive 135 comment(s)
  • 28 Coolest Firefox About:Config Tricks 126 comment(s)

Enjoyed the article? Subscribe to Make Tech Easier today to get your daily updates of technology tutorials, tips and tricks.

Enter your email:

  • Graham
    this is for the Star Wars & Red Dwarf fans out there. I was bored so for fun I put together this prompt. I know it's nothing much, but it suits me :D. Contains mostly codes from here (and the top text is from the 'figlet' program)

    Code : http://pastebin.com/QuLzm7HG

    On a side note, is it possible to get the clock in 12 hour format (would be nice to have am/pm status after it too)
  • axelon
    Just to clarify for MacOS X:

    If you want to apply these changes permanently, you don't need to search or create a .bashrc file, just append the .profile file in you home directory, since MacOS reads this file for environment variables
  • Carmen
    I love the happy face one!
  • Damian Montaldo
    When I use the number 2 I can't copy remote files using scp.
    I can do an ssh but I can't copy files, why is that?

    Its very weird because I use that prompt for almost a week :)
    and I lost a two days looking for the error!

    I need some help, here is the scp debug:
    debug1: Next authentication method: password
    password:
    debug1: Authentication succeeded (password).
    debug1: channel 0: new [client-session]
    debug1: Entering interactive session.
    debug1: Sending environment.
    debug1: Sending env LANG = en_US.UTF-8
    debug1: Sending command: scp -v -t /tmp
    ..........
    I'm stuck here.

    Ps: Solved using the old PS1 bash prompt :)
  • k00pa
    Thanks! I love these! Now I am using number 2 :)

    It's hard to decide what use now...
  • Gavitron
    I use this on most of my accounts, the first line sets the terminal window to read "hostname:username - workingdir" an absolute must when you've got 20+ xterm/aterm/rxvt windows open to multiple machines, in one taskbar.

    PS1="\[33]0;\h:\u - \w07\]\
    \[33[1;37m\][\
    \[33[1;35m\]\$(date +%H:%M) \
    \[33[1;36m\]\h\
    \[33[1;31m\]:\
    \[33[1;32m\]\w\
    \[33[1;37m\]]\
    \[33[0m\] "
  • Amit Agarwal
    how about something like this

    if [[ \$? = "0" ]]; then color="\\[\33[32m\\]"; else color="\\[\33[31m\\]"; fi
    you can put this condition to get the color to set and use the variable color in #8 to set the color.
  • rainer
    can somebody help me to combine #8 and #2 ?
    i like the information provided by #8 but also the gimick turning red on a bad command of #2: so how would the code look like for a version of number 8 where all the green parts are turning red after a bad command? can t figure it out on my own.
  • The UnDisbeliever
    A lot of interesting ideas here, I'm gonna have to change my .bashrc in the immediate future.

    For the moment this is my bash prompt, after accidentally rebooting my laptop instead of my server for the eighth time in 12 hours, I decided to have separate command promts for a local login and a SSH login:


    if [ -z "$SSH_CLIENT" ]; then
    PS1='\[\e[0;32m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[1;32m\]\$ \[\e[m\]\[\e[0;37m\]'
    else
    PS1='\[\e[0;32m\]\u\[\e[m\]\[\e[1;31m\]@\[\e[m\]\[\e[1;33m\]\h\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[1;32m\]\$ \[\e[m\]\[\e[0;37m\]'
    fi

    As far as I know (I haven't done any in depth searching), not many people have done separate prompts for different logins. Does anyone know of anyone else who does this?

    P.S. Sorry for the late comment, I always seam to arrive late to these blog posts. Why does nobody tell me anything until AFTER it happens?
  • Joshua Price
    I haven't seen anyone else change prompt based on SSH status, but many people have one prompt for root and another for users.

    Regarding the delay in finding/commenting, I'd recommend a feed reader to keep active on your favorite blogs. Nearly all blogs (including MTE) have RSS feeds you can send to something like Google Reader.
  • Ivan Andrus
    Another (superior IMHO) way to allow copy/paste of a whole line is a prompt that looks something like

    : your info here ;

    e.g.

    : /path/[vc rev] : exit status ;

    because the : is the bash command that does nothing and the ; terminates that command and allows your command to execute. I use it since I hate multi-line prompts (no offense to those who like them of course).
  • Jeremiah Altepeter
    I use a multi line prompt that begins with a # as such:

    PS1="\n#--[\[\e[1;36m\]\u@\h\[\e[m\]]-[\[\e[1;34m\]\w\[\e[m\]]-[\$(date +%k:%M)]-->\n"

    This prompt displays as:

    #--[user@host]-[/path/to/working/dir]-[13:46]-->
    (with the cursor being down here on a new line)

    This prompt gives me (IMHO) the most important info at a quick glance, what machine I'm on, where I am in it, and the times each of my commands started and finished.

    The real treat in this prompt, is that I can copy a whole screen worth of terminal and just paste it into a shell script with no editing necessary: the # at each prompt line comments it out!

    Try it, you'll like it, or your money back!
  • Joshua Price
    I had considered putting something like that in the article originally, but couldn't think of a good way to demonstrate the point with screenshots. Thanks!
  • Vide
    The #7 is *really* expensive resources wise if used in any server environment. Use it with caution and carefully or you may be in trouble someday when less expected (although an unset PS1; exec bash will solve your problem)
  • bits of info
    This is awesome!
  • Chris Adams
    Continuing the version-control discussion, I posted the bash function which displays the current branch for Git, Mercurial, Subversion and even RCS:

    http://improbable.org/chris/index.php?ID=227
  • Dave
    Regarding the line characters ? and ? in #7

    They are from the Microsoft cp437 codepage but have been mapped to unicode.
    See: http://en.wikipedia.org/wiki/Cp437

    So as long as you are using a utf-8 enabled terminal (the default on most modern distros) - and your terminal fonts are set up correctly - you can just copy and paste them.

    PS1="\n\[\e[30;1m\]?(\[\e[34;1m\]\u@\h\[\e[30;1m\])-(\[\e[34;1m\]\j\[\e[30;1m\])-(\[\e[34;1m\]\@ \d\[\e[30;1m\])->\[\e[30;1m\]\n??(\[\e[32;1m\]\w\[\e[30;1m\])-(\[\e[32;1m\]$(/bin/ls -1 | /usr/bin/wc -l | /bin/sed 's: ::g') files, $(/bin/ls -lah | /bin/grep -m 1 total | /bin/sed 's/total //')b\[\e[30;1m\])--> \[\e[0m\]"

    Note: There is superfluous "\[" after the first dash on the second line in the original code given above.
  • Dave
    Ha ha...
    They don't show up here either...
    Just replace the ?s in the line I gave above with the appropriate characters copied from http://en.wikipedia.org/wiki/Cp437

    Also, you can generate them directly in your terminal with:
    echo -e "\xe2\x94\x8c"
    echo -e "\xe2\x94\x94"
    And I used a long dash
    echo -e "\xe2\x94\x80" which looks better in most terminals.
  • maciek
    My favorite is: PS1='C:${PWD//\//\\\}>'

    Which looks like this:
    C:\home\maciek\>
  • John Anderson
    That's just sick. LOL! :)
  • Raymond
    Use termcap or terminfo for escape secuences.
  • Raymond
    It is basically imbecile to put terminal-dependent escape sequences into prompts. For some reason terminfo and termcap were invented.

    Raymond
  • David
    I just found this article and decided to piece together from what I could figure out from these examples

    PS1="\[\e[37;1m\]-{\[\e[34;1m\]\u@\h\[\e[37;1m\]}-\n\[\e[37;1m\](\[\e[34;1m\]\w: \$(/bin/ls -1 | /usr/bin/wc -l | /bin/sed 's: ::g') files, \$(/bin/ls -lah | /bin/grep -m 1 total | /bin/sed 's/total //')b\[\e[37;1m\])\n--> \[\e[0m\]"

    It's a three line prompt. Name and host at the top, directory in the middle and prompt on the last line.
  • Amit Agarwal
    I use bashstyle for this purpose.
    http://blog.amit-agarwal.co.in/2009/06/17/bashs...
  • Maertin
    After reading this i took the time to make my promt, and remembered why i am using zsh.
    even though zsh syntax is easier to read i took the idea from one of the /. replies and made this file which i then source from .zshrc
    ------------------------setpromt-zsh-----------------------
    RESET_COLOR="%f"
    COLOURED_RETURN_VAL="%(?,%F{green},%F{red})(%?)"
    SHORT_PWD="%3~"
    ROOT_OR_NOT="%#"
    WHITE="%F{white}"

    # Throw it all together
    PROMPT="${COLOURED_RETURN_VAL}${WHITE}${SHORT_PWD}${ROOT_OR_NOT}>${RESET_COLOR}"
    RPROMPT="%F{yellow}%T%f[%!]"
    -----------------------------EOF-------------------------

    gives me this
    (0)~%> 0:36[3081]
    (0)~%>return 123 0:37[3081]
    (123)~%> 0:37[3082]

    you can't see the colors here
    but the return number turns red when not zero
  • Michael
    OK, so after dinking around for a while -- I've come up with one I like. I'd be even happier if I could figure how to get the line characters to work (like in #7)...

    Anyway, this one is based on parts of #1 and #7. It has user@machine, timestamp and directory one one line -- and job count, a success/failure indicator (a green :-) on success or a red :-( on failure) on the second line. followed by a $

    PS1="\n\[\e[30;1m\](\[\e[34;1m\]\u@\h\[\e[30;1m\])-(\[\e[34;1m\]\t\[\e[30;1m\])-(\[\[\e[32;1m\]\w\[\e[30;1m\])\[\e[30;1m\]\n(jobs:\[\e[34;1m\]\j\[\e[30;1m\])\`if [ \$? -eq 0 ]; then echo \[\e[32m\] \:\-\); else echo \[\e[31m\] \:\-\( ; fi\`\[\e[0m\] $ "

    # Now I must get some work done :-)
  • rexmo
    bash: syntax error near unexpected token `\[\e[34'
  • Michael
    Thanks to Joshua Price's reply above, I can reduce the frustration level of anybody wanting to try the PS1 I posted -- by providing a link to a non-mangled version:

    http://media.stray-hound.com/prompt.sh
  • Joshua Price
    Perhaps it didn't paste right, it's invalid when I try to test.

    As noted in comments above, bash codes don't always display correctly, not sure if it's our CMS or some browsers or what. If you've got a link to a plain text version or something, please share.
  • Jedediah Smith
    Find here my bash prompt that displays git branch and changed files:
    http://code.extension.ws/post/181469611/git-pro...
  • Joshua Price
    Running a ruby script for each prompt display is pretty bold =)

    It is a nice prompt though!
  • Volkan YAZICI
    Check this out: http://www.students.itu.edu.tr/~yazicivo/files/...
  • Travis
    Lol, #1 is definitely my favorite!
  • mark
    People seem to be happy, so I will be the first to say that those prompts are horribly ugly.

    Bash is ugly!

    PS: Zsh is not that much more beautiful but the RPROMPT really is damn cool.
  • Alexander Fairley
    What do I need to do to get the lovely interstitial characters on prompt 7 to display?
  • Joshua Price
    I had mixed success with that. Initally it worked perfect for me in xterm and wterm, but looked broken in gnome-terminal. After I tinkered with it, it no longer looked right in any term!

    I would suggest seeing if it looks correct in another terminal (like xterm or wterm)
  • jrglasgow
    PS1="\[33[01;32m\]\u@\h\[33[01;34m\] \w \[33[00m\]\$(git branch 2>/dev/null | grep '^*' | colrm 1 2)\[33[01;34m\] \$\[33[00m\] "

    The interesting thing about my prompt is the git repository branch is named in the prompt
  • Joshua Price
    Thanks, I had considered adding a nice git prompt I found but didn't think all that many people would be interested so I'm glad someone added it in comments.

    Also, for other readers of jrglasgow's comment, see the comment above regarding broken escape codes
  • Cygal
    My zsh prompt is quite small.

    PROMPT='%~> '
    RPROMPT='%T'

    Displays the canonical path on the left, and the time on the right. If the command is too long, the time is removed.

    ~> cd ../ 9:56
    /home> 9:56
  • mike
    http://slashdot.org/comments.pl?sid=108424&...

    this is the earliest version i can find.
  • mike
    http://slashdot.org/comments.pl?sid=108424&...

    this is the earliest reference i can find
  • Zilioum
    found out. sry, didnt read the all of the first paragraph.
  • Zilioum
    How can I get rid of those commmands again (don't worry, I like them, but I like to know how to uninstall something before I install it)
    Cheers
  • seventoes
    If you don't put it in your .bash_profile or .bashrc file, it won't load during your next session, so just close the window. Otherwise, just remove it from the file you put it in.
  • Joshua Price
    Right, if you paste one of these in your terminal without saving in something like .bashrc it will go away next time you open your terminal. You can also type "source .bashrc" to restore the default prompt.
  • robb
    LOL smiley command.
    nice one.
  • Craig
    Nice, thanks!

    I accidentally leave stuff backgrounded a bit often, so I've used the jobs concept above to get it to OPTIONALLY report the bg jobs if there are any..

    PROMPT_COMMAND='PS1="${debian_chroot:+($debian_chroot)}\[33[01;32m\]\u@\h\[33[00m\] \[33[01;34m\]\W\[33[00m\] `if [ $(jobs | wc -l | tr -d " ") -gt 0 ]; then echo "\[\e[1;31m\]jobs:\j\[\e[m\]"; fi` \$ \e]30;\h\a"'

    craig@homei7 tmp jobs:1 $
    craig@homei7 tmp jobs:1 $ fg
    vi /tmp/tmp1
    craig@homei7 tmp $
  • Joshua Price
    Thanks for the suggestion Craig.

    For anyone wanting to try Craig's prompt, there's a minor annoyance when posting these kinds of codes where any instance of "backslash zero" gets filtered out, so all cases of "\[33" should actually be "\[\ 033" (extra space added to make sure it doesn't happen on this reply)
  • Greg
    Great stuff, I like the concepts behind all of them, think I might try out the happy face!

    In the future though, you should use png for images of text, instead of jpg. They'll look a lot cleaner.
  • EllisGL
    Now how do you show the extended char set via a mingetty sshd session?
blog comments powered by Disqus

Our Writers

  • Damien Oh - Chief Editor
  • Joshua Price
  • Shevonne Polastre (FreeAgentWriter)
  • Tavis J. Hampton (Lantern Torch)
  • Trevor Dobrygoski
  • Soumen Halder (Ampercent)
  • Sharninder Khera (Geeky Ninja)
  • Tanmay Ahmed (Tech-Tips-Geek)
  • Write for us

Latest Posts

  • How to Create Space Scenes Quickly and Easily in Gimp
  • GeekTool: Turn Your Mac’s Desktop Into An Information Center
  • XMind: A Cross-platform Mind-mapping Solution
  • Get 1-Click File Sharing And Backup Features With Tonido + Free Giveaway
  • Paragon Backup: Another Great Backup Software For Windows
Make Tech Easier on Facebook
Jobs by SimplyHired
Job Widgets

Copyright

All the articles (including the text, images and tutorials) are properties of MakeTechEasier.

You cannot copy whole tutorials, either in English or translate to another language without our permission.

Write for us

Writing a tutorial or article for MakeTechEasier is a great way to get exposure and give back to the community.

Read More Details

Suggestions

We need your help to help us serve you better! This is YOUR site, so if you have suggestions/feedbacks on how we can improve it for you, please let us know! We do our best to make this place better!

Make a Suggestion

Navigation

  • Home
  • About Us
  • Advertise
  • Jobs
  • Archives
  • Contact Us

Copyright © 2007-2010 Make Tech Easier | All Right Reserved.