exit status of last command in bash prompt

hakerdefo

I know there are gazillion examples out there on the net about this but almost all of them use 'if-then-else' to achieve this. Which in my view is not very efficient. The one I'm going to present here uses built-in arithmetic option of bash shell and there for is more efficient and Eco-friendly  ;)
If the last command you ran in the terminal failed this will turn '$' ['#' for root] in bash prompt (PS1) an angry red like this,


But if the command was successful $' ['#' for root] will remain peaceful white,


If you think this is cool then all you need to do is open '.bashrc' file in your favorite text-editor and just copy-paste the following at the end of the file,

export PS1='${debian_chroot:+($debian_chroot)}\[\e[01;32m\]\u \[\e[01;37m\]> \[\e[01;33m\]\w\n\[\e[$(((($?>0))*31))m\]\$\[\e[0m\] '

Save the file and run the following in your terminal to apply the changes,

source .bashrc

Cheers!!!
You Can't Always Git What You Want