How to stop shell script if curl failed

Solution 1:

You can check for exit code using $?:

exit_status = $?
if [ $exit_status != 0 ]
  then
    exit $exit_status
fi

If you want to analyze exit status, take a look at the Exit codes section from man curl page. There are a lot of different codes, depending on why it failed.

EDIT : You can use command1 || command2 as well. command2 is executed if and only if command1 has failed:

curl .... || exit 1

Solution 2:

Just exit if curl ends with a non-zero exit code:

curl http://www.example.com || exit 1

Or, make your script exit on error:

set -e
curl http://www.example.com