As an update to my earlier article, a friend gave me an idea of how to make my $PS1 even better… First, the relevant part of my ~/.bashrc:
ps1_prompt() {
local ps1_exit=$?
if [ $ps1_exit -eq 0 ]; then
#ps1_status=`echo -e "\[\033[32m\]"'\$'"\[\033[0m\]"`
ps1_status='\$'
else
ps1_status=`echo -e "\[\033[1;31m\]"'\$'"\[\033[0m\]"`
fi
ps1_git=''
if [ "$(__git_ps1 %s)" != '' -a "$(__git_ps1 %s)" != 'master' ]; then
ps1_git=" (\[\033[32m\]"$(__git_ps1 "%s")"\[\033[0m\])"
fi
PS1="${debian_chroot:+($debian_chroot)}\u@\h:\[\033[01;34m\]\w\[\033[00m\]${ps1_git}${ps1_status} "
}
# preserve earlier PROMPT_COMMAND entries...
PROMPT_COMMAND="ps1_prompt;$PROMPT_COMMAND"
If you haven’t figured it out, the magic is that the trailing $ prompt gets coloured in red when the previous command exited with a non-zero value. Example:
james@computer:~$ cdmkdir /tmp/ttboj # yes, i built cdmkdir james@computer:/tmp/ttboj$ false james@computer:/tmp/ttboj$ echo ttboj ttboj james@computer:/tmp/ttboj$ ^C james@computer:/tmp/ttboj$ true james@computer:/tmp/ttboj$ cd ~/code/puppet/puppet-gluster/ james@computer:~/code/puppet/puppet-gluster$ # hack, hack, hack...
You can still:
$ echo $?
42
if you want more specifics about what the exact return code was, and of course you can edit the above ~/.bashrc snippet to match your needs.
Hopefully this will help you be more productive, I know it’s helping me!
Happy hacking,
James