Color shifting bash prompt based on exit code

Here is my PS1 variable to get a green bash prompt if the previous command exited successfully and a red one otherwise.
Based on the Smiley Face Bash Prompt.
Reference to bash colours: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html

PS1="${debian_chroot:+($debian_chroot)}\`if [ \$? = 0 ]; then echo '\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '; else echo '\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '; fi\`"

Edit your ~/.bashrc script:

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes

and

if [ "$color_prompt" = yes ]; then
  #  PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
     PS1="${debian_chroot:+($debian_chroot)}\`if [ \$? = 0 ]; then echo '\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '; else echo '\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '; fi\`"
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi