Take command of your BASH prompt


[Joshua] has put together a list of BASH prompt customizations. The command prompt is used in a command-line interface to show that the system is ready for the next command. Often times this is nothing more than a user name, host name, and working directory:


[Joshua's] customization examples can be used to color code the information in your prompt, change what information is displayed, and make the prompt respond differently when an invalid command is typed. A BASH prompt reference is helpful in deciphering what each of these commands do. The easiest simplification is to understand that non-printing characters (such as color codes) are surrounded in escaped square brackets. For example, line 1 is the sequence for Red, line 2 is the sequence for Dark Grey, and line 3 sets a simple prompt to display in Red and all text after that to be in Dark Grey:

PS1="\[\e[0;31m\]\u@\h:\w\$ \[\e[1;30m\]"


Above you will notice an added notification that we typed an invalid command. This prompt is produced with the following code:

PS1="\`if [ \$? != 0 ]; then echo \[\e[33m\]---=== \[\e[31m\]Oh noes, bad command \[\e[33m\]===---; fi\`\n\[\e[1;30m\]XX \[\e[0;32m\]Hack a Day \[\e[1;30m\]XX\n\[\e[0;37m\][\[\e[1;31m\]\@\[\e[0;37m\]] \[\e[0;32m\]\u@\h \[\e[0;37m\][\[\e[1;34m\]\w\[\e[0;37m\]] \[\e[0;32m\]\$ \[\e[0m\] "

We use the shell a lot and this transforms the prompt from something we mostly ignore into a useful tool. Typing a command in the shell will change the prompt for the current session only. If you want a more permanent change, add the line to the bottom of your ~/.bashrc file.

[via Digg]


  1. bandwaggon says:

    BASH, so… avergage. If you wanted better promtage, you’d most likely be using zsh. Its really a shame more people dont know of the wonders that zsh has, so many people dragooned into mediocrity.

  2. anon says:

    a digg article about ricing up the bash prompt. a new low hack a day…

  3. f|_|ckr1c3rs says:

    i hate ricers, they suck

  4. blitzcat says:

    Capital letters! on hack a day? say it ain’t so!

  5. tauntaun_rider says:

    Wow, you’re right, we have capital letters now! Everything was so comfortable to read I didn’t notice!

  6. alvare says:

    Hey that’s a good idea, now my “$” at the end of the prompt indicates the output of the last command with it’s color xP

  7. Zencyde says:

    Oh holy crap, capital letters! FINALLY! My punctuation and grammar will not go unnoticed!

  8. guns says:

    NEWLINES!!! Break up your prompt with a newline to ensure that the input caret is always in the same spot:

    [HOSTNAME] guns [/path/inside/a/really/deep/nested/directory/tree/so/long/that/you/can't/see/your/command/prompt] $ your_command_here


    HOSTNAME [/path/inside/a/really/deep/nested/directory/tree/so/long/that/you/can't/see/your/command/prompt]
    guns $ your_command_here

    Add a ‘\n’ to your $PS1 for better usability. Most terminal emulators will handle it just fine.

    It’s like [Fitts' law][1] for CLIs.

    [1]: http://en.wikipedia.org/wiki/Fitts%27s_law

  9. anonymoose says:

    I wrote an even better article on customizing the bash prompt:

    “man bash”

    This should be tomorrow’s article…

  10. ellisgl says:

    Here’s a couple of cool ones:



    I have #7 from the second link on one of my boxes.

    Anyone know how to get putty to do the IBM “Extended ASCII” or how to get Linux to output it over Mingetty? Tried all sorts of TERM and LANG settings to no avail.

  11. amk says:

    i don’t think my keyboard supports capital letters. do i need an arduino for that?

  12. lovro says:

    this is so not a hack, that i had to write something. all the other things that aren’t hacks but something else, they should not be here, but ok.
    this is not a hack, this is a tutorial, and week one at that…

  13. Neckbeard says:

    I wonder if I could control and Arduino with Bash … In all seriousness stop knocking this most Linux newbies can barely dribble their way through apt-get this could be useful for them.

  14. andrew says:

    maybe an arduino or two could help said newbies

  15. Richard says:

    @ ‘The Haters’ – we were all newbies once, and quite a few people reading will still be battling their way up the Linux learning-curve.

    Sometimes your lame ‘leet’ egos seem to forget that, so get over it already.

    Rather than complaining when somebody offers newbies a hand up, why don’t you do something constructive and encourage them too?

  16. lovro says:

    I am not saying that you should not help someone that does not know something, but this is not the place to teach someone three lines of bash. if you are a linux newbie and can’t find this in google than you should stay a newb.

  17. maddhat says:

    Why is this article on hack a day? I must say I don’t enjoy this move recently to just digg/reddit tech stories versus hacks. I hold hack a day to a higher standard.

  18. Not sure this is really Hack a Day material. Its not really a hack, simply utilising commands that are already available. I got this working on windows a couple of years ago – kept me amused for about five minutes.

  19. Eric says:

    now this is just my opinion, but its non-hack bullshit like this that makes a person like eliot want to leave. i don’t know the real story, or even if there is one, but i sure a hell would leave too if this is the best that hack-a-day can come up with. this is so _not_ a hack. i’ll tell you what, mr. mike sczycs, you tell me how this is a hack, or even remotely a hack, and i’ll take it all back. oh and one other thing __boycott capitals__–who’s with me?

  20. yosh says:

    @anonymoose: lol, you won the game ^^

  21. Fuzzy says:

    All I see in the comments is a lot of bitching about how hackaday is going down hill.

    Here’s an idea get off your fat asses and provide a few links, I’m surprised anyone’s stuck around with you lot posting holier-than-thou comment crap each day.

  22. fringe says:

    actually this _is_ a hack. I think every kind of customisation is a kind of hack. Would it have been better if they posted about a bash sourcecode hack that implemented a new modular way of handling the prompt? with latest tweets, arduino reporting when coffee machine is ready, … ?

  23. PidGin128 says:


    C:\Users\PidGin128>set prompt=$P$_$G

    >prompt /? …..


    I also enjoy breaking it onto a newline, so this is in my system environ, I just hadn’t got that far on this install yet. I think $M is remote share, so sometimes I include that, but I don’t really remap like mad– most of my shares are static.

    Also, since like you said guns, this is the result of deep paths, I think we are violating so other basic tennant that I can’t put my finger on.

    I am on both sides of this argument, I don’t like seeing it on hackaday, but equally, it’s not so obvious to many people. It would be nice to prioritize hardware stuff, but I’m guilty of the not submitting myself, so….

    Anyways, on to the future!

  24. Josh says:

    You could also consider using fish:


    Works like a heavily customized zsh from the first install. Perhaps not as fast as bash or zsh, but computers are getting faster every day…

  25. 16Bitiges Bit says:

    Is there something in this add-on which zsh is not able to do?

  26. sol says:

    There’s nothing more pathetic than posting on a forum about how much the forum sucks and you aren’t coming back. Only slightly less pathetic is complaining day after day about how the quality has gone down over the last few years but not taking the simple (and potentially free) steps necessary to put up a few pictures and text explaining your own “hack.”

  27. TALR says:

    Hah, this is funny because I was just about to go look for some interesting things to do for $PS1 ^_^

    Very nice

  28. TALR says:

    And @ “all complainers” – Wtf is with you guys whining about this? I mean, it’s not like hackaday is going to, or _has to_ for that matter, come out with an “o-day” everyday. That’s just not going to ever happen. So why not just sit back, and relax.

    And btw, all the “wise” complainants in the room should know what a hack IS.
    You mod a system?
    You hacked your system.
    You mod your shell?
    You hacked your shell.
    You only change 1 variable in some source?
    You hacked the source code.

    Size doesn’t matter- and if you’re going to whine about it, then maybe you need to re-evaluate your “hacker” status, because whining is not what hackers do.

  29. alex says:

    HAHA….Very basic stuff here. You have to be a complete idiot not to already know how to do these things. Way to funny

  30. Mark says:

    For me personally, the -= bad command =- is far too verbose. I colour-code my $ symbol like so:

    \[33[01;$([ $? -eq 0 ] && echo 34 || echo 31)m\]\$ \[33[00m\]

    So it’s blue (like the preceeding text) when a command returns zero and red otherwise.

  31. science says:

    The funny thing to me about people ridiculing “newbies”, is that in my experiences, the people who feel it necessary to do so are typically only slightly less “new” than those they attempt to insult.

    Anyone who has been involved with this stuff long enough has better things to do than try to act “leet” in random comment sections.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 96,401 other followers