31

I want to do something like:

if [[ git status &> /dev/null ]]; then
   echo "is a git repo";
else
   echo "is not a git repo";
fi

except I don't know how to check the exit status. How do I fix this?

guntbert
  • 536
  • 6
  • 19
anon
  • 41,035
  • 53
  • 197
  • 293

3 Answers3

38

The variable $? contains the last commands return code

EDIT: precise example:

git status &> /dev/null
if [ $? -eq 0 ]; then
  echo "git status exited successfully"
else
  echo "git status exited with error code"
fi
kevin.groat
  • 1,274
  • 12
  • 21
orip
  • 73,323
  • 21
  • 116
  • 148
  • Oh right, forgot, >& does work, sometimes, depending on how ambiguous the syntax is -- it's a bad habit, though, and better to use &> which is not ambiguous. Sorry. – Phil P Mar 22 '10 at 03:42
  • 1
    Just to be clear, `git status` can return a non-zero exit code for a lot more reasons than the absence of a git repo. – erb Nov 04 '14 at 10:43
  • Of course. This answer attempts to address the OP's zsh question about return codes, I completely agree that the git relevance is minimal. Perhaps I'll edit it. – orip Nov 04 '14 at 20:17
26

Simply like that

if git status &> /dev/null
then
   echo "is a git repo";
else
   echo "is not a git repo";
fi

Or in a more compact form:

git status &> /dev/null && echo "is a git repo" || echo "is not a git repo"
gregseth
  • 12,952
  • 15
  • 63
  • 96
1

Another form that I often use is the following:

git status &> /dev/null
if (( $? )) then
    desired behavior for nonzero exit status
else
    desired behavior for zero exit status
fi

This is slightly more compact than the accepted answer, but it does not require you to put the command on the same line as in gregseth's answer (which is sometimes what you want, but sometimes becomes too hard to read).

The double parentheses are for mathematical expressions in zsh. (For example, see here.)

Edit: Note that the (( expression )) syntax follows the usual convention of most programming languages, which is that nonzero expressions evaluate as true and zero evaluates as false. The other alternatives ([ expression ], [[ expression ]], if expression, test expression, etc.) follow the usual shell convention, which is that 0 (no error) evaluates as true and nonzero values (errors) evaluate as false. Therefore, if you use this answer, you need to switch the if and else clauses from other answers.

sasquires
  • 356
  • 3
  • 15
  • 2
    You actually don't need the `$` for variables inside arithmetic evaluation. See http://www.bash2zsh.com/zsh_refcard/refcard.pdf: "`var` (does not require `$` in front unless some substitution e.g. `${#var}` is needed, `$` is error if `var` is to be modified)" – ericbn Sep 13 '18 at 14:28
  • 2
    Please note that this has the *opposite* effect of the above answers, where it will go into the if block if the return code is non-zero, and into the else block if it is zero – kevin.groat Sep 15 '19 at 18:02
  • @kevin.groat Thanks, I forgot to mention that. Adding it now. – sasquires Sep 19 '19 at 19:30