Skip to content

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 fails
  • set -u view undefined variable as error
  • set -x run in debug mode
  • set -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
  1. License under CC BY-NC 4.0
  2. Copyright issue feedback me#imzye.me, replace # with @
  3. Not all the commands and scripts are tested in production environment, use at your own risk
  4. No privacy information is collected here
Try iOS App