Debugging
In this lesson, you will learn about bash flags useful for debugging, how to trace bash code, and useful resources for linting bash scripts.
How Important is this Lesson?
If you write, use or maintain bash of any complexity you’ll want to know how to debug it!
Syntax Checking Options
Start by creating this simple (but broken) script:
cat > debug_script.sh << 'END'#!/bin/bashA=some valueecho "${A}echo "${B}"END
Now run it with the -n
flag. This flag only parses the script, rather than
running it. It’s useful for detecting basic syntax errors.
bash -n debug_script.sh
You can see it’s broken. Fix it. Then run it:
bash debug_script.sh
You’ll see:
[1]+ Done sleep 60
in the terminal.
Again, it reports the job number, this time with the status (Done
), and the command
that was originally run (sleep 60
).
Controlling Jobs
Just like starting jobs, you can control jobs by sending signals to them.
Here you’re going to start two jobs, one to sleep for two minutes, and the next for one second more (so we can distinguish between them).
bash -v debug_script.sh
Try tracing to see more details about ...