
Interesting Bash Prompts - fogus
http://maketecheasier.com/8-useful-and-interesting-bash-prompts/2009/09/04
======
iigs
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.

~~~
whimsy
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#...](http://www.faqs.org/docs/Linux-HOWTO/Bash-Prompt-
HOWTO.html#AEN784)

~~~
there
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.

------
judofyr
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

~~~
viraptor
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.

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

~~~
barrkel
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

~~~
gaius
It's on Blastwave:
[http://www.blastwave.org/pkg/pkgcontents.ftd?software=zsh...](http://www.blastwave.org/pkg/pkgcontents.ftd?software=zsh&style=brief&state=5&arch=i386)

    
    
        # pkgutil --install zsh

~~~
astine
As well as on SunFreeware:
<http://www.sunfreeware.com/programlistsparc10.html#zsh>

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

------
ashleyw

        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>

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

------
davidw
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) $

~~~
extension
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

~~~
lvv
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>

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

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

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

------
thristian
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

------
10ren
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>

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

