I implemented a “debugger” for Bash in six lines of Bash. It kind of
debugger keyword. Here’s how it works:
And there it is. Add this to a script, insert a call to
somewhere, and run the script. It’ll pause right execution right there.
Once paused, we can do things like:
- print the contents of variables with
- run commands that are on our
- call functions defined in the script
… and pretty much everything that we could have done if we were editing the script directly. Here’s a short session demonstrating how it can be used:
Stopping on failures
I find that most of the time this is useful when a script is failing for
some reason. Rather than put a
debugger call right before the failing
command, I can just add this at the top of the file:
When any command has a non-zero exit code, Bash will run
pause the program.
I’ve been keeping this function and
trap call commented out at the top
of my scripts and uncommenting them when needed (It uses
is not the best from a security perspective, which is why it’s commented
Of course, I said “debugger” in quotes earlier because it’s not really a debugger:
Using it requires editing the script we want to debug to include these lines, and then calling
debuggersomewhere. It doesn’t launch an inferior process and control it, like
breakcommand to edit breakpoints while stopped. All breakpoints must have been written into the program up front.
There’s also no
nextcommands for stepping into or over the next function or command.
When it stops, it doesn’t show the text content of the last line that executed, or even the line number.
But I have some thoughts on how to implement these, too… Bash’s
builtin has a way to trap
DEBUG, which runs after every command. I
think I could make clever use of
traps to implementat least one of
next, and definitely something that says “stopped on line X”
and maybe even use that to print the source text of that line.
break seems to be the hardest—I don’t have any ideas for
that one right now.
I’m releasing this code into the public domain. If you want to change it to implement any of these features, I’d be more than interested to hear about it!