spacer

8 Useful and Interesting Bash Prompts

By Joshua Price | September 4, 2009 |

spacer Many 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:
spacer
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:
spacer
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:
spacer
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:
spacer
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:
spacer

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:
spacer

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:
spacer

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



Print this pageSave as PDF

Josh Price is a senior MakeTechEasier writer and owner of Rain Dog Software
  • How To Display Hidden Files and Folders In Windows
  • Random Post
  • Mac: How to Create a Basic Script With Automator
  • Pingback: Eustáquio Rangel (taq) 's status on Friday, 04-Sep-09 14:53:31 UTC - Identi.ca

  • EllisGL

    Now how do you show the extended char set via a mingetty sshd session?

    • EllisGL

      Oh how to get it to work in putty?

    • ellisgl

      I’m sure it’s something with the RHEL 5 environment and the encoding it’s sending. Any ideas? I’ve gone a bunch of LANG and TERM settings on the server side…

    • ellisgl

      I’m sure it’s something with the RHEL 5 environment and the encoding it’s sending. Any ideas? I’ve gone a bunch of LANG and TERM settings on the server side…

  • EllisGL

    Now how do you show the extended char set via a mingetty sshd session?

    • EllisGL

      Oh how to get it to work in putty?

    • ellisgl

      I’m sure it’s something with the RHEL 5 environment and the encoding it’s sending. Any ideas? I’ve gone a bunch of LANG and TERM settings on the server side…

  • firsthour.net/ 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.

  • firsthour.net 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.

  • 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;ha”‘

    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)

  • 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)

  • robinferianto.com/ robb

    LOL smiley command.
    nice one.

  • robinferianto.com/ robb

    LOL smiley command.
    nice one.

  • 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

    • insomniaaddict.com/ 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.

  • 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

    • insomniaaddict.com 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.

  • Zilioum

    found out. sry, didnt read the all of the first paragraph.

  • Zilioum

    found out. sry, didnt read the all of the first paragraph.

  • Pingback: Take command of your BASH prompt - Hack a Day

  • Pingback: Take command of your BASH prompt | Mobile Jewels

  • Pingback: 8 Useful and Interesting Bash Prompts – Make Tech Easier « Netcrema – creme de la social news via digg + delicious + stumpleupon + reddit

  • mike

    slashdot.org/comments.pl?sid=108424&cid=9219400

    this is the earliest reference i can find

  • mike

    slashdot.org/comments.pl?sid=108424&cid=9219400

    this is the earliest reference i can find

  • mike

    slashdot.org/comments.pl?sid=108424&cid=9219400

    this is the earliest reference i can find

  • mike

    slashdot.org/comments.pl?sid=108424&cid=9219400

    this is the earliest version i can find.

  • mike

    slashdot.org/comments.pl?sid=108424&cid=9219400

    this is the earliest version i can find.

  • Pingback: links for 2009-09-05 .:: [aka ????????] Ozver.in | ???????

  • Pingback: Take command of your BASH prompt | Diy all the Way

  • 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

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

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.