How to detect if a git clone failed in a bash script

bash, git, linux, shell

Solution

Here are some common forms. Which is the best to choose depends on what you do. You can use any subset or combination of them in a single script without it being bad style.

if ! failingcommand
then
    echo >&2 message
    exit 1
fi
failingcommand
ret=$?
if ! test "$ret" -eq 0
then
    echo >&2 "command failed with exit status $ret"
    exit 1
fi
failingcommand || exit "$?"
failingcommand || { echo >&2 "failed with $?"; exit 1; }

Problem

How can I tell if a `git clone` had an error in a bash script? ``` git clone git@github.com:my-username/my-repo.git ``` If there was an error, I want to simply `exit 1`;

Original source