Color your terminal return codes

Any program you run outputs a return-code when it stops to tell the system whether it was successfull (including ‘terminal commands’ like ls, which is actually also a program). I use the terminal a lot so I appreciate things that make it even better – like for example coloring the terminal return codes that can be seen in the screenshot below. 

Image

The color that prefixes the input field reflects the return code of the last command executed. 

It was inspired by a comment this blog about coloring the terminal shell.
You get it by adding the following to your .bashrc (afterwards you have to restart your terminal) 

# Success / failure indications for return code of job.
OLDPS1=$PS1
PASS=' '
FAIL=' '
SEGV='X'
CLEAR='\e[0m'
RED="${CLEAR}\[\e[41m\]"
GREEN="${CLEAR}\[\e[42m\]"
WHITE="\[\e[0m\]"

PS1="\`RC=\$?; if [ \$RC = 0 ]; then echo ${GREEN}${PASS}${WHITE}; elif [ \$RC = 139 ]; then echo ${RED}${SEGV}${WHITE}; else echo ${RED}${FAIL}${WHITE}; fi\` $OLDPS1"
Advertisements

2 thoughts on “Color your terminal return codes

  1. Thanks for sharing the terminal-goodness! I should mention that the original code I posted should be credited to Annirak.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s