title: How do I set $? in functions called by PS1?
body: <p>I currently have a prompt in bash that calls a function to output the return code of the last command run (if non-zero):</p>
  
  <pre class="lang-bsh prettyprint-override"><code>exit_code_prompt()
  {
      local exit_code=$?
      if [ $exit_code -ne 0 ]
      then
          tput setaf 1
          printf "%s" $exit_code
          tput sgr0
      fi
  }
  
  
  PS1='$(exit_code_prompt)\$ '
  </code></pre>
  
  <p>This works rather nicely, except for <code>$?</code> not resetting unless another command is run:</p>
  
  <pre><code>$ echo "works"
  works
  $ command_not_found
  bash: command_not_found: command not found
  127$ 
  127$ 
  127$ 
  127$ echo "works"
  works
  $
  </code></pre>
  
  <p>Is it possible to reset/unset the value of <code>$?</code> for the parent shell the first time <code>exit_code_prompt()</code> is run such that it does not continue to repeat the value in the prompt?</p>
  
  <p>Many thanks, Steve.</p>