Shuhei Kagawa

Color prompt by exit code

Oct 18, 2015 - Bash

The idea of customizing bash prompt hooked me a few months ago. I tried a few crazy things like showing random emojis and finally settled down with just showing the current directory, git branch and status.

At Tokyo Node Gakuen #18 on October 8th, @yosuke_furukawa talked about Node.js v4.0. Aside from the well-organized and informative talk, I found another interesting thing at his demo. His terminal prompt turned red when a command failed.

It looked pretty and useful. So I emulated it.

Changing prompt color

The code in .bash_profile is pretty straightforward. It changes the prompt color depending on whether the exit code is 0 or not. The only one trick is to capture the exit code at the very first line of the prompt command to prevent it from being changed in prior lines.

# Colors
light_green="\[\e[1;32m\]"
light_red="\[\e[1;31m\]"
yellow="\[\e[0;33m\]"
gray="\[\e[0;37m\]"
reset="\[\e[m\]"

# Customize prompt
prompt_command() {
  local status="$?"
  local status_color=""
  if [ $status != 0 ]; then
    status_color=$light_red
  else
    status_color=$light_green
  fi
  export PS1="[${yellow}\w${reset}]${gray}$(__git_ps1)${reset} ${status_color}λ${reset} "
}
export GIT_PS1_SHOWDIRTYSTATE=1
export PROMPT_COMMAND=prompt_commandsh

One of the advantages of attending real events over just browsing slides online is being able to take a peek of other people's dev environments. Looking forward to seeing more cool stuff on upcoming events like Tokyo Node Fest 2015.