Finding Bugs

Finding Bugs: Debugging Scripts: bash -x, ShellCheck: A Second Pair of Eyes, Testing Scripts with bats.

Updated

  1. Debugging Scripts: bash -x (Journeyman): bash -x runs a script and prints every line before it runs, with its variables already filled in, so you can watch what the script really did; set -x and set +x trace one part only, and PS4 adds line numbers to the trace.
  2. ShellCheck: A Second Pair of Eyes (Journeyman): ShellCheck reads a script without running it and points out likely mistakes, each with its line, a code such as SC2086 and a plain message; the codes worth knowing are an unquoted variable, a cd that might fail and a loop over ls, and a disable comment silences one warning on purpose.
  3. Testing Scripts with bats (Freelancer): A test is a small script that runs yours and checks the result; bats runs a file of them, each written with @test, run and checks on $status and $output, in a fresh folder from setup, and reports each one as passed or failed, with the line that failed.