Debug in bash
To run bash scripts in a safer way, here are some tips and usage patterns to debug bash scripts.
command line options
bash -x ## debug
bash -n ## test syntax
set options
set -e
exit immediately when a command failsset -u
view undefined variable as errorset -x
run in debug modeset -o pipefail
This tells the script that if any of the steps (not in a block construction) fail (exit with non-zero), the whole script should halt in place and the script will exit with the failure message of the failed step.
Put them together
set -euxo pipefail
Disclaimer
- License under
CC BY-NC 4.0
- Copyright issue feedback
me#imzye.me
, replace # with @ - Not all the commands and scripts are tested in production environment, use at your own risk
- No privacy information is collected here