Skip to content

Instantly share code, notes, and snippets.

@caffeinetiger
Last active April 18, 2022 18:20
Show Gist options
  • Select an option

  • Save caffeinetiger/89d5f87d88d4e3acf2787640f26e1d6d to your computer and use it in GitHub Desktop.

Select an option

Save caffeinetiger/89d5f87d88d4e3acf2787640f26e1d6d to your computer and use it in GitHub Desktop.
**References** - [Exit When Any Command Fails](https://intoli.com/blog/exit-on-errors-in-bash-scripts/)
#!/bin/bash
exit_on_error() {
exit_code=$1
last_command=${@:2}
if [ $exit_code -ne 0 ]; then
>&2 echo "\"${last_command}\" command failed with exit code ${exit_code}."
exit $exit_code
fi
}
some_command args
exit_on_error $? !!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment