Every script breaks eventually. A typo in a variable name, a missing file, a command that fails halfway through. The difference between a throwaway script and one you can trust is how it behaves when something goes wrong: whether you can find the bug quickly, and whether the script stops safely instead of carrying on and doing damage.
This module starts with exit codes, the signal every
command gives you about success or failure. Next you will learn to
debug with tracing using bash -x
and set -x to see exactly what the shell runs.
Then ShellCheck, a linter that catches common mistakes
before you run the script. The module finishes with
trap, for cleaning up temporary files and
logging a result however your script exits.
Try each technique on a script of your own as you watch the lessons in order. When you are done, head to Projects to put the whole course into practice.
How to tell whether a command succeeded or failed using exit codes, and how to check them in your own scripts.
Nick Janetakis shows how bash -x and set -x print a trace of commands and variables, and uses it to track down a script that works by hand but fails when run.
How ShellCheck finds subtle and obvious bugs, from quoting mistakes to variable typos, and how to ignore a warning or run it in continuous integration.
How trap lets you delete temporary files or write a log entry however a script ends, including Ctrl+C and SIGTERM, plus why SIGKILL cannot be trapped.