DevOps Zone is brought to you in partnership with:

I have been working for almost two years now on infrastructure and deployment automation, exploring programmatic solutions to traditional systems administration problems and configuration management. I'm fanatical about testing, the scientific method and building good tools to support awesome   Oliver is a DZone MVB and is not an employee of DZone and has posted 29 posts at DZone. You can read more from them at their website. View Full User Profile

Cool, interesting, useful, unique and innovative Shell Prompts

10.10.2012
| 5805 views |
  • submit to reddit

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?

Published at DZone with permission of Oliver Hookins, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Andrzej Grzesik replied on Thu, 2012/10/11 - 5:30am

\[\033[0;34m\][\[\e[0;36m\]$(date +%H:%M)\[\033[0;34m\]]\[\033[0;34m\][\[\033[0;32m\]\u\[\033[1;38m\]@\[\e[0;36m\]\h:\[\033[0;34m\]\w\[\033[0;32m\]$(parse_git_branch)\[\033[0;34m\]]\[\033[0;32m\]\n$

 Which gives me a: 

Mitch Pronschinske replied on Thu, 2012/10/11 - 7:26am in response to: Andrzej Grzesik

Nice one Andrzej!

Mike P(Okidoky) replied on Thu, 2012/10/11 - 11:40am

LOL. Has anyone ever written a compiler to generate all that mess? I mean, it looks worse than Lisp/Scheme even!

Andrzej Grzesik replied on Thu, 2012/10/11 - 12:04pm

I generate my PS with below. Once you alias colours, it's a bit more approachable.

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 resetPrompt {
  local        BLUE="\[\033[0;34m\]"
  local         RED="\[\033[0;31m\]"
  local   LIGHT_RED="\[\033[1;31m\]"
  local       GREEN="\[\033[0;32m\]"
  local LIGHT_GREEN="\[\033[1;32m\]"
  local       WHITE="\[\033[1;37m\]"
  local        GRAY="\[\033[1;38m\]"
  local        TEAL="\[\e[0;36m\]"
  case $TERM in
*)
TITLEBAR=""
;;
esac

if [ 0 -eq ${UID} ]; then
  export PS1="${TITLEBAR}\
$BLUE[$RED\$(date +%H:%M)$BLUE]\
$BLUE[$RED\u$GRAY@$TEAL\h:$BLUE\w$GREEN\$(parse_git_branch)$BLUE]\
$GREEN\n\$"
else
 export PS1="${TITLEBAR}\
$BLUE[$TEAL\$(date +%H:%M)$BLUE]\
$BLUE[$GREEN\u$GRAY@$TEAL\h:$BLUE\w$GREEN\$(parse_git_branch)$BLUE]\
$GREEN\n\$ "
fi

export PS2='> '
export PS4='+ ' 
} 

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.