in zsh, how do I do a conditional on the exit status of a program?

anon picture anon · Feb 1, 2010 · Viewed 14k times · Source

I wnat 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 do checking on the exit status. How do I fix this?

Thanks

Answer

orip picture orip · Feb 1, 2010

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