Testing and debugging shell scripts
Quick answer
Test a script with known input and expected output, status and filesystem changes. Syntax checks, static analysis and execution tests answer different questions.
Prepare the practice environment
Run python3 lab-fixtures/shell-scripting/setup.py as an ordinary user from the extracted package root in a disposable Linux environment. Enter the printed directory, check pwd, then run export LC_ALL=C; umask 077; set -o pipefail. Use a fresh fixture for each example. Save the code as example.sh inside that directory and run bash --noprofile --norc -o pipefail example.sh rather than sourcing it. Stop if a command fails unexpectedly. All output paths must be new. The source of check-report.sh is in the fixture JSON. Outputs below are not commands.
How it works
bash -n script.sh parses without executing ordinary commands; it detects syntax errors, not incorrect logic. bash -x traces expanded commands to stderr and can expose sensitive values; use only with synthetic data. ShellCheck analyzes common shell mistakes when installed, but neither a clean lint result nor a syntax check guarantees correctness. Test a valid filename, a missing one and a filename with spaces. The fixture check-report.sh accepts exactly one argument, returns 2 for usage errors and 1 for a missing regular file.
Use a small test table for check-report.sh: a valid existing file with a space in its name should succeed, a missing file should return 1, and no arguments should return 2. Capture stdout and stderr separately because they serve different consumers. A syntax check may pass for all three cases while the script still returns the wrong status. If an assertion fails, reduce the input to the smallest case that reproduces the failure before enabling tracing with fictional data.
Try the examples
The syntax check does not run the script.
Expected stdout:
syntax OKbash check-report.sh 'daily report.txt'if bash check-report.sh absent.txt 2> error.log; then printf 'unexpected success\n' exit 1else printf 'status=%s\n' "$?"ficat error.logCheck stdout, stderr and failure status separately.
Expected stdout:
found: daily report.txtstatus=1missing fileRequires ShellCheck; not installed here. Review any findings instead of suppressing them blindly.
Manual example; not executed locally. Follow its stated environment restrictions.
Common mistakes
Never run tracing with secrets present. A syntax-valid script can still return the wrong status or destroy data.
Practice
Run check-report.sh with no arguments, a valid file and a missing file. Compare output and status; optionally run bash -x on the synthetic valid example and explain what the trace shows.
Related references
Study: bash, shellcheck. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Testing and debugging shell scripts — Knowledge Check
Apply the lesson concepts to the controlled shell fixture.
What does bash -n verify?
Why avoid bash -x with secrets?
What should a test assert besides stdout?
What does ShellCheck provide?