adrift on a cosmic ocean

Writings on various topics (mostly technical) from Oliver Hookins and Angela Collins. We have lived in Berlin since 2009, have two kids, and have far too little time to really justify having a blog.

Cool, interesting, useful, unique and innovative Shell Prompts

Posted by Oliver on the 19th of September, 2012 in category Tech
Tagged with: promptshell

At $employer today, we had our bi-weekly tech talk session and one of the lightning talks given was on tmux. Tmux is an excellent piece of software (although I gave up on it and started using iTerm2) but that's not what I wanted to talk about.

One of the other participants in the session noticed the presenter's shell prompt had a little smiley/frowney face which changed both expression and colour depending on the exit code of the last command - how cool is that? How many times have we all typed echo $? just to find out if our last command was really successful? It really makes sense to have this information displayed at all times.

So in that spirit I'm sharing my PS1 prompt variable with you. It's not the most advanced, doesn't use all of the bells and whistles and I'm still not entirely sure the information it presents is essential but it's a work in progress. I'd love for you to share your own in the comments in the hope of spreading know-how and ideas!

export PS1="`if [ $? = 0 ]; then echo e[33\;40m\\^\\_\\^e[0m; else echo e[36\;40m\\\-e[0m\\_e[36\;40m\\\-e[0m; fi` \[\033[38m\]u \[\033[0;36m\]j \[\033[1;32m\]\!\[\033[01;34m\] w \[\033[31m\]`ruby -e "print (%x{git branch 2> /dev/null}.split(%q{n}).grep(/^\*/).first || '').gsub(/^\* (.+)$/, '(\1) ')"`\[\033[37m\]$\[\033[00m\] "

Roughly in order, this equates to:

  1. Smiley/frowney face based on exit code of last command.
  2. Username
  3. Number of backgrounded jobs
  4. Shell history number
  5. CWD leaving home directory an unexpanded ~
  6. Git repository branch, using Ruby 1.8/1.9-compatible code

On my machine it looks like this:


What does yours look like?

© 2010-2018 Oliver Hookins and Angela Collins