title: How do I test for a non-zero exit status of a command in Bash?
body: <p>I have a script that checks the exit status of the following function:</p>
  
  <pre><code>function is_git_repository {                                                    
      git branch &amp;&gt; /dev/null                                                     
  }  
  </code></pre>
  
  <p>Which returns <code>0</code> if you're in a git repo, and <code>128</code> if you're not. </p>
  
  <p>I have no problem testing to see if the return value is <code>0</code>; the following works as expected:</p>
  
  <pre><code>if is_git_repository ; then 
      echo you are in a git repo
  else 
      echo you are NOT in a git repo
  fi
  </code></pre>
  
  <p>But it's when I'm trying to test for an exit status that is anything OTHER than <code>0</code> when I'm running into problems. I've tried the following, but none of them work:</p>
  
  <ol>
  <li><code>if [[ "$(is_git_repository)" != "0" ]] ; ...</code> always evaluates to true (<a href="https://stackoverflow.com/a/6241286/1094541">link</a>)</li>
  <li><code>if [[ "$(is_git_repository)" -ne "0" ]] ; ...</code> always evaluates to false</li>
  <li><code>if [[ "$(is_git_repository)" != 0 ]] ; ...</code> always evaluates to true</li>
  <li><code>if [[ "$(is_git_repository)" -ne 0 ]] ; ...</code> always evaluates to false</li>
  <li><code>if [[ ! "$(is_git_repository)" ]] ; ...</code> always evaluates to true</li>
  <li><code>if !is_git_repository ; ...</code> just echoes the command back to me, but without the bang (wtf?) </li>
  </ol>
  
  <p>What is the correct way to check for a non-zero exit status of a command in an if statement?</p>