title: How to change bash prompt color based on exit code of last command?
body: <p>I want to display a green smile face if the previous exit code is 0 and red smile face if not successful. </p>
  
  <p>Basically I want to do this prompt but with other stuff included. </p>
  
  <pre><code>PS1='\u@\h:\w `if [ $? = 0 ]; then echo \[\e[32m\]:\)\[\e[37m\]; else echo \[\e[31m\]:\(\[\e[37m\]; fi` $ '
  </code></pre>
  
  <p>I want to abstract the condition logic to a function but when I try to combine these two the escape characters show instead of the colors.</p>
  
  <pre><code>smiley()                                                                       
  {
      if [ $? == 0 ]; then
          echo ':)'
      else
          echo ':('
      fi
  }
  RED="\033[1;5;91m"
  GREEN="\033[1;5;92m"
  NONE="\033[m"
  NORMAL="\[\033[0m\]"
  YELLOW="\[\033[1;4;93m\]"
  MAGENTA="\[\033[35m\]"
  WHITE="\[\033[1;37m\]"
  BLINK="\[\033[5m\]"
  #INVERT="\[\e[7m\]"
  #OFF="\[\033[m\]"
  
  PS1="${YELLOW}\u${MAGENTA}@${YELLOW}\h${NORMAL}:${WHITE}\w $(smiley)\n"
  </code></pre>
  
  <p>I even tried one line but it didn't work either. </p>
  
  <pre><code> PS1='\[\033[1;4;93m\]\u\[\033[35m\]@\[\033[1;4;93m\]\h\[\033[0m\]\[\033[1;37m\]    \W if [ $? = 0 ]; then echo \[\e[32m\]:\)\[\e[37m\]; else echo \[\e[31m\]:\(\[\    e[37m\]; fi\n'
  </code></pre>
  
  <p>If there any way to do this without PROMPT_COMMAND?</p>