Simple bash debugger using trap DEBUG

Inspired by this post by @tminnigaliev.

Besides traps(handlers) for signals, bash have 4 special traps:

  • EXIT to run on exit from the shell.
  • RETURN to run each time a function or a sources script finishes.
  • ERR to run each time command failure would cause the shell to exit if set -e is used.
  • DEBUG to execute before every command.

The last one allows to create a simple debugger inside a bash script:

function _trap_DEBUG() {
    echo "# $BASH_COMMAND";
    while read -r -e -p "debug> " _command; do
        if [ -n "$_command" ]; then
            eval "$_command";

trap '_trap_DEBUG' DEBUG

Now before executing every command, it is printed and we have a command prompt. There we can print any command to execute in the script context or an empty line to continue.

Also it is possible to run another script in this debugging mode without modifying it. The only catch is that bash functions and scripts inlined by source do not inherit DEBUG, RETURN and ERR traps. We can use set -T to allow inheriting DEBUG and RETURN traps.

Here is such a simple debugger with some bells and whistles added: selivan/bash-debug

Also it is worth mentioning that a gdb-like debugger for bash scripts exists: bashdb. It has way more functionality but requires building from source and installation.

License: This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License Share: share on reddit share on hacker news share on facebook share on twitter share on linkedin share on slashdot