Commit | Line | Data |
---|---|---|
4e4793ff | 1 | Functions to deal with errors in bash |
8a786097 | 2 | |
26cc217e | 3 | errcatch: set a trap on error to do bash-trace and exit |
bf8b73a0 IK |
4 | errallow: undo errcatch |
5 | errexit: exit and print stack trace | |
26cc217e | 6 | bash-trace: print stack trace (best bash stack trace ever) |
7c47da50 | 7 | |
bf8b73a0 IK |
8 | the err* functions depend on bash-trace. |
9 | ||
276299a5 | 10 | For scripts without any nested function calls, this is usually sufficient: |
bf8b73a0 IK |
11 | set -eE -o pipefail |
12 | trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR | |
8a786097 | 13 | |
d52bee6b IK |
14 | Cleanup commands or functions can be appended to the ERR trap after a |
15 | semicolon within the single quotes. | |
16 | ||
4e4793ff | 17 | Related: see my bash script template repo, at https://iankelling.org/git. |
bf8b73a0 | 18 | |
276299a5 IK |
19 | Please email me if you have a patches, bugs, feedback, or republish this |
20 | somewhere else: Ian Kelling <ian@iankelling.org>. |