Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Interesting Bash Prompts (maketecheasier.com)
59 points by fogus on Sept 5, 2009 | hide | past | favorite | 30 comments


Some of these are pretty cool. One piece of experience I'd like to offer:

Don't ever use ">" in your prompts, particularly as root.

A coworker had spent several days working on* an important script with a tight deadline that was almost upon him, and with a quick slip of the right mouse button, pasted what he had selected before (several lines of window scrollback) into the window to be executed, including:

/tmp/foo> ./blah.pl

and just like that, blah.pl ended up zero length. We hard killed the machine and scanned the disk for the contents of the script and recovered most of it, but had the specifics been different we could have been a lot less lucky.

For this reason it's probably also a good idea to avoid question marks, asterisks, and anything else the shell could expand, as well.

* While the story plays just as well, it's not meant to be an argument for source control. It could have just as easily been full paths to system binaries and have ended up trashing the system.


If you're particularly worried about this sort of thing, you can wrap your prompt in : and ; i.e. : user@host $ ;

Source is http://www.faqs.org/docs/Linux-HOWTO/Bash-Prompt-HOWTO.html#...


or use something like 'set noclobber' (at least in tcsh)

    jcs@smug:~% echo asdf > file
    jcs@smug:~% echo fdsa > file
    file: File exists.
    jcs@smug:~% echo fdsa >! file
    jcs@smug:~% 
this has saved me quite a few times when accidently typing > instead of >> or similar.


I'd argue that this isn't really a prompt problem, though.

For example, when using typical Unix terminals, I refuse to mouse copy/paste when logged in as root (whether or not the prompt is nearby), because it's too easy to make a mistake and the consequences are severe. Similarly, I refuse to rely on any cute shell tricks like "!" to pull history items when logged in as root. Sacrifice convenience to guarantee safety.

I also prefer to use my Mac terminal (MacTelnet), which will trap multi-line pastes and offer options to paste as-is, paste as one line, or cancel the operation. This has saved me more than once.


Perhaps the real lesson to be learned here is don't ever login as root (?).

(Certainly there are a few things I don't know about administering large groups of machines. However, I've been running my own personal Linux boxes for a while. And the last time I logged in as root was a bit over 9 years ago.)


I have a very simple prompt on my dev machine:

* I don't need the hostname. The lack of it = my machine.

* I don't need the username. I'm the only user.

* I don't need the full path. I know my machine pretty well. If I'm in doubt, pwd is half a second away.

* The prompt is red so I easily can scan what the output of this specific command was.

* Current Git-branch = helps me not make stupid mistakes.

  gitprompt() {
    PS1="\[\e[0;31m\]\W$(__git_ps1 " \[\e[1;31m\](%s)") \$ \[\e[m\]"
  }
  PROMPT_COMMAND=gitprompt


I'm tried to minimise the length of "user@host fullpath" too. This is what I ended up with (in zsh):

    PS1='[%(?..%?)%(?.. ! )$PR_GREEN%2>>%n%<<@%m$PR_NO_COLOR $PR_BLUE%30<...<%~%<<$PR_NO_COLOR]%(!.#.$) '
path is limited to 30 characters and the front is trimmed with '...'. There is a host, because I work on at least 5 at the same time every day, but the username is cut to only the first 2 letters (so it's either vi, or ro, or something else at uni). The return code is only visible when it's non-zero.


If you enjoy Bash's customization, You'll love Zsh's

http://aperiodic.net/phil/prompt/

Once you go bash->zsh, there is no turning back.


I'd have to rely on zsh being available everywhere:

    [rupert] ~$ uname -a
    SunOS rupert 5.11 NexentaOS_20081207 i86pc i386 i86pc Solaris
    [rupert] ~$ sudo apt-get install zsh
    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    Package zsh is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source
    E: Package zsh has no installation candidate


My Solaris 10 box has it installed by default:

  Last login: Sun Sep  6 12:01:35 2009 from 192.168.1.113
  Sun Microsystems Inc.   SunOS 5.10      Generic January 2005
  bash-3.00$ zsh
  unknown%
That said, I prefer Fish when I can get it: http://fishshell.org/index.php


It's on Blastwave: http://www.blastwave.org/pkg/pkgcontents.ftd?software=zsh...

    # pkgutil --install zsh



Seconded. I stuck to bash for a long time, and I regret not changing to zsh much earlier. It's like bash in all the ways bash is good, but with extra excellence thrown in to the bargain.


I'd heard this many times, but for some reason your comment actually made me decide I should probably listen.

Opened up zsh, and it throws me into a new-user setup thing, stepping me through a bunch of different configuration options. I'm impressed so far. :P


I especially like the bash history number in the prompt, makes it much easier to use the history effectively. Perhaps if I used jobs rather than tabs I'd throw the job count in there too. But multi-line prompts? that seems a bit excessive.


    function parse_git_dirty {
      [[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"
    }
    function parse_git_branch {
      git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/"
    }
    function parse_current_dir {
      ruby -e "puts ('../'+Dir.getwd.split('/').last(2).join('/')).gsub('//', '/')"
    }

    export PS1='\[\033[31;40m\]$(parse_current_dir)\[\033[0;33m\]$(parse_git_branch)\[\e[0m\]$ '
I couldn't be bothered to work out how to shorten the path natively, so I hacked it [badly] ]in ruby... :)

http://grab.by/3Gg


Hm, I think, that prompt should give you some hint of the environment and context in which the command you are typing will run. Username, working path, machine hostname, at least, of course (well, it isn't really necessary to show hostname, if you never use more than one machine, and so on). On the other hand, I don't like stuff like current time, number of running processes or system load, because they are not part of that context. They are constantly changing, and not only by actions and commands in the shell; when you are done typing and press enter, they won't be valid.


I changed mine for the first time in years, to show me which git branch I'm in, to avoid doing the wrong thing in the wrong place.

    davidw@fortrock:~/workshop/langpop (master) $


+1 for the git branch.

Here's my bash prompt script. It shows the command number from this session (PS_NUM), the exit code of the last command (highlighted red if != 0), the pwd (in cyan), and the git branch (in magenta)

    OFF="\[\033[0m\]"
    RED="\[\033[0;31m\]"
    MAGENTA="\[\033[0;35m\]"
    CYAN="\[\033[0;36m\]"

    function promptcommand {
        STATUS=$?
    
        PS_NUM="[\#]"
        PS_STATUS="[$STATUS]"
        PS_PWD="${CYAN}\w${OFF}"
        PS_GITBRANCH="${MAGENTA}\$(__git_ps1)${OFF}"
        PS_PROMPT="\$"
    
        if [ $STATUS -ne "0" ]; then
            PS_PROMPT="${RED}${PS_PROMPT}${OFF}"
        fi
    
        PS1="${PS_NUM}${PS_STATUS} ${PS_PWD}${PS_GITBRANCH} ${PS_PROMPT} "
    }
    PROMPT_COMMAND=promptcommand
I probably could have eliminated the PROMPT_COMMAND thing like in the first example.


I use vcprompt, it works with all the major ones, and is written in C to be fast: http://vc.gerg.ca/hg/vcprompt/


Mine shows git branch and changed file status: http://code.extension.ws/post/181469611/git-prompt-rb

It's a Ruby script so not the fastest in theory, but I haven't noticed any delay using Ruby 1.9


There is faster version of this. In pure bash. And can do much more, including GIT, SVN and HG. Screen shots: http://volnitsky.com/project/git-prompt


Here's mine for Mercurial (shows branch and changeset number): http://gist.github.com/181764


Here's a version that automatically DTRT for git, hg, and svn (hg last in implementation so my home doesn't override other repositories): http://bitbucket.org/durin42/dotfiles/src/tip/.shell.d/50.vc...

Here's the code for the zsh prompt that uses it: http://bitbucket.org/durin42/dotfiles/src/tip/.shell.d/99.pr...


If you like that, perhaps you will like these:

export GIT_PS1_SHOWDIRTYSTATE=1

export GIT_PS1_SHOWSTASHSTATE=1

export GIT_PS1_SHOWUNTRACKEDFILES=1


Reminds me of colorful prompts I used to show off in DOS by device=ansi.sys, (I think - It has been, what, more than 20 years?). Save the current cursor location, go to the top of the screen, show the date, time, working directory (in different colors of course, left, middle and right positions) and then restore the cursor location and show a prompt. In yet more ansi colors.


I'm a ZSH user myself, preferring a simple [user@host#dir], but my favorite amusing BASH prompt is:

  PS1='C:${PWD//\//\\\}>'


In a prior life I used zsh and learned to love its show-current-directory-trimmed-to-length prompt code, but these days I've succumbed to inertia and use bash.

With much messing around, I managed to figure out how to replicate the same functionality in bash:

    http://screwtape.jottit.com/how_to_limit_the_length_of_your_bash_prompt


The ascii art in 7, using angle graphics, is very appealing to me. It looks compellingly like a FSM (though that's not how the eg is using it).

http://images.maketecheasier.com/2009/08/bashprompts-7.jpg


It's interesting that folks are still displaying info in prompts.

With TTYs and their CRT equivalents, there really wasn't any alternative. However, most folks have their command line in a window these days, and windows have other places to put state information.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: