Commit | Line | Data |
---|---|---|
acd03e1e | 1 | Bash stack trace and error handling functions |
8a786097 | 2 | |
acd03e1e IK |
3 | Usually, just copy the err file and source it. Then on any non-zero |
4 | return code outside of a conditional, the program will exit and print a | |
5 | stack trace. | |
78a1a75c | 6 | |
acd03e1e IK |
7 | The err file has some functions which you may want to call manually for |
8 | different or additional error handling. See the comments at the start of | |
9 | each one. | |
997eb0be | 10 | |
acd03e1e IK |
11 | For scripts where functions are not used, you can optionally use a more |
12 | minimal approach: | |
bf8b73a0 IK |
13 | set -eE -o pipefail |
14 | trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR | |
8a786097 | 15 | |
acd03e1e IK |
16 | In the above line, cleanup commands or functions can be appended to the |
17 | ERR trap after a semicolon within the single quotes. | |
d52bee6b | 18 | |
4e4793ff | 19 | Related: see my bash script template repo, at https://iankelling.org/git. |
bf8b73a0 | 20 | |
276299a5 IK |
21 | Please email me if you have a patches, bugs, feedback, or republish this |
22 | somewhere else: Ian Kelling <ian@iankelling.org>. |