title: Capturing exit value of previous command in bash prompt
body: <p>How do I capture the exit value of the previous command in my bash prompt? I want to do this because I also want to include the current git branch in my path as well, and that changes <code>$?</code></p>
  
  <p>I have tried quite a few variations on the following prompt (e.g.  `, ` and $() around the assignment to RETVAL), but RETVAL is empty in all of them:</p>
  
  <pre><code>PS1="\$(RETVAL=$(echo \$))\$(cd 124123)retval: $RETVAL"
  </code></pre>
  
  <p>The cd command inside is just a placeholder for the call to the git current branch function, and I am echoing RETVAL afterwards (this is always an empty string to test if it gets assigned.</p>
  
  <p>Other questions that are similar/relevant:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/7135824/creating-a-bash-command-prompt-with-a-red-after-failure-of-previous-command">Creating a Bash command prompt with a red $ after failure of previous command</a> - </li>
  <li><a href="https://stackoverflow.com/questions/16715103/bash-prompt-with-last-exit-code">Bash prompt with last exit code</a></li>
  </ul>
  
  <p>I have also tried setting RETVAL in the prompt command using examples like on these pages, but nothing is displayed in the prompt</p>