Last active
April 18, 2022 18:20
-
-
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/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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