3.5 Script/Debugger Interaction

The BASH debugger and your program live in the same variable space so to speak. BASH does not have a notion of module scoping or lexical hiding (yet) as is found in modern programming languages and in modern versions of the Korn shell. This then imposes some additional care and awareness.

Most of the variables and functions used inside the BASH debugger start _Dbg_, so please don’t use variables or functions with these names in your program.

Note: there are some other variables that begin with just an underscore (_); over time these will be phased out. But until then, avoid those or consult what is used by the debugger. Run ‘bashdb --debugger -c "declare -p"’ to list all the variables in use including those used by the debugger.

A number of environment variables are also reserved for use; these start with DBG_. For example: DBG_INPUT, DBG_LEVEL and, _Dbg_QUIT_ON_QUIT (see Debug), DBG_RESTART_FILE (see Starting), to name a few. Finally, there are some BASH environment dynamic variables and these start with BASH_. For example BASH_SUBSHELL (see Debug), BASH_COMMAND (see Command Display), BASH_LINENO, and BASH_SOURCE to name a few.

Inside the debugger some variables may be redefined. In particular IFS and PS4, and various dollar variables $?, $1, $2, etc. The values before entering the debugger are saved and those variables have their old values restored when leaving the debugger. However you may notice these difference in various debugger commands. For example examine PS4 might not return the same value as eval declare -p PS4. The former is picking the debugger value while the eval is careful to restore the value to what it was before entering the debugger.

In order to do its work The BASH debugger sets up a DEBUG trap. Consequently a script shouldn’t reset this or the debugger will lose control. The BASH debugger also sets up an EXIT handler so that it can gain control after the script finishes. Another signal intercepted is the an interrupt or INT signal. For more information about signal handling, see Signals