Visualize the Return Code of Your Last Command via the Prompt
I like to have a pretty obvious indication of the success of failure of any commands I might have run. To accomplish this I add several color coding shortcuts to my .bash_profile
:
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NOFORMAT='\033[m'
#return code visualization
RVAL='$(RET=$?; if [ $RET -eq 0 ]; then echo -ne "$GREEN"; else echo -ne "$RED"; fi; echo -ne "$RET")'
Using those I can make a relatively simple $PS1
that I add to the end of my .bash_profile
:
# prompt
export PS1="$BLUE\t $NOFORMAT\h $BLUE\w $RVAL\r\n\\\$ "
That gives me a prompt that looks like this for root after a successful command:
11:57:24 MyBox /etc
0
# ▋
# ▋
or like this for a normal user after a failed command (CTRL-C
in this case):
11:57:27 MyBox ~/git
0
$ ▋
$ ▋
I personally don't put any color changes after the newline \n
. It tends to lead to funky things when scrolling through your history or dealing with really long commands.