Take Command Of Your BASH Prompt

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

mike@krusty:~$

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

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

bad_command_prompt

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]

32 thoughts on “Take Command Of Your BASH Prompt

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

    VS

    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

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

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

  5. @ ‘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?

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

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

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

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

  10. —–

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

    C:\Users\PidGin128
    >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!

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

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

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

  14. 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 to FuzzyCancel reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.