title: Bash loop until a certain command stops failing
body: <p>I would like to write a loop in bash which executes until a certain command stops failing (returning non-zero exit code), like so:</p>
  
  <pre><code>while ! my_command; do
      # do something
  done
  </code></pre>
  
  <p>But inside this loop I need to check which exit code <code>my_command</code> returned, so I tried this:</p>
  
  <pre><code>while ! my_command; do
      if [ $? -eq 5 ]; then
          echo "Error was 5"
      else
          echo "Error was not 5"
      fi
      # potentially, other code follows...
  done
  </code></pre>
  
  <p>But then the special variable <code>?</code> becomes <code>0</code> inside the loop body.
  The obvious solution is:</p>
  
  <pre><code>while true; do
      my_command
      EC=$?
      if [ $EC -eq 0 ]; then
          break
      fi
      some_code_dependent_on_exit_code $EC
  done
  </code></pre>
  
  <p>How can I check the exit code of <code>my_command</code> (called in loop header) inside loop body without rewriting this example using a <code>while true</code> loop with a break condition as shown above?</p>