This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new!

Feature: Tools & Utilities

Debug your shell scripts with bashdb

By Ben Martin on November 24, 2008 (6:00:00 PM)

Share    Print    Comments   

The Bash Debugger Project (bashdb) lets you set breakpoints, inspect variables, perform a backtrace, and step through a bash script line by line. In other words, it provides the features you expect in a C/C++ debugger to anyone programming a bash script.

To see if your standard bash executable has bashdb support, execute the command shown below; if you are not taken to a bashdb prompt then you'll have to install bashdb yourself.

$ bash --debugger -c "set|grep -i dbg" ... bashdb<0>

The Ubuntu Intrepid repository contains a package for bashdb, but there is no special bashdb package in the openSUSE 11 or Fedora 9 repositories. I built from source using version 4.0-0.1 of bashdb on a 64-bit Fedora 9 machine, using the normal ./configure; make; sudo make install commands.

You can start the Bash Debugger using the bash --debugger syntax or the bashdb command. The former method is recommended except in cases where I/O redirection might cause issues, and it's what I used. You can also use bashdb through ddd or from an Emacs buffer.

The syntax for many of the commands in bashdb mimics that of gdb, the GNU debugger. You can step into functions, use next to execute the next line without stepping into any functions, generate a backtrace with bt, exit bashdb with quit or Ctrl-D, and examine a variable with print $foo. Aside from the prefixing of the variable with $ at the end of the last sentence, there are some other minor differences that you'll notice. For instance, pressing Enter on a blank line in bashdb executes the previous step or next command instead of whatever the previous command was.

The print command forces you to prefix shell variables with the dollar sign ($foo). A slightly shorter way of inspecting variables and functions is to use the x foo command, which uses declare to print variables and functions.

Both bashdb and your script run inside the same bash shell. Because bash lacks some namespace properties, bashdb will include some functions and symbols into the global namespace which your script can get at. bashdb prefixes its symbols with _Dbg_, so you should avoid that prefix in your scripts to avoid potential clashes. bashdb also uses some environment variables; it uses the DBG_ prefix for its own, and relies on some standard bash ones that begin with BASH_.

To illustrate the use of bashdb, I'll work on the small bash script below, which expects a numeric argument n and calculates the nth Fibonacci number.

#!/bin/bash version="0.01"; fibonacci() { n=${1:?If you want the nth fibonacci number, you must supply n as the first parameter.} if [ $n -le 1 ]; then echo $n else l=`fibonacci $((n-1))` r=`fibonacci $((n-2))` echo $((l + r)) fi } for i in `seq 1 10` do result=$(fibonacci $i) echo "i=$i result=$result" done

The below session shows bashdb in action, stepping over and then into the fibonacci function and inspecting variables. I've made my input text bold for ease of reading. An initial backtrace (bt) shows that the script begins at line 3, which is where the version variable is written. The next and list commands then progress to the next line of the script a few times and show the context of the current execution line. After one of the next commands I press Enter to execute next again. I invoke the examine command through the single letter shortcut x. Notice that the variables are printed out using declare as opposed to their display on the next line using print. Finally I set a breakpoint at the start of the fibonacci function and continue the execution of the shell script. The fibonacci function is called and I move to the next line a few times and inspect a variable.

$ bash --debugger ./ ... (/home/ben/testing/bashdb/ 3: version="0.01"; bashdb<0> bt ->0 in file `./' at line 3 ##1 main() called from file `./' at line 0 bashdb<1> next (/home/ben/testing/bashdb/ 16: for i in `seq 1 10` bashdb<2> list 16:==>for i in `seq 1 10` 17: do 18: result=$(fibonacci $i) 19: echo "i=$i result=$result" 20: done bashdb<3> next (/home/ben/testing/bashdb/ 18: result=$(fibonacci $i) bashdb<4> (/home/ben/testing/bashdb/ 19: echo "i=$i result=$result" bashdb<5> x i result declare -- i="1" declare -- result="" bashdb<7> print $i $result 1 bashdb<10> break fibonacci Breakpoint 1 set in file /home/ben/testing/bashdb/, line 5. bashdb<11> continue Breakpoint 1 hit (1 times). (/home/ben/testing/bashdb/ 5: fibonacci() { bashdb<(12)> next (/home/ben/testing/bashdb/ 6: n=${1:?If you want the nth fibonacci number, you must supply n as the first parameter.} bashdb<(13)> next (/home/ben/testing/bashdb/ 7: if [ $n -le 1 ]; then bashdb<(14)> x n declare -- n="2" bashdb<(15)> quit

Notice that the number in the bashdb prompt toward the end of the above example is enclosed in parentheses. Each set of parentheses indicates that you have entered a subshell. In this example this is due to being inside a shell function.

In the below example I use a watchpoint to see if and where the result variable changes. Notice the initial next command. I found that if I didn't issue that next then my watch would fail to work. As you can see, after I issue c to continue execution, execution is stopped whenever the result variable is about to change, and the new and old value are displayed.

(/home/ben/testing/bashdb/ 3: version="0.01"; bashdb<0> next (/home/ben/testing/bashdb/ 16: for i in `seq 1 10` bashdb<1> watch result 0: ($result)==0 arith: 0 bashdb<2> c Watchpoint 0: $result changed: old value: '' new value: '1' (/home/ben/testing/bashdb/ 19: echo "i=$i result=$result" bashdb<3> c i=1 result=1 i=2 result=1 Watchpoint 0: $result changed: old value: '1' new value: '2' (/home/ben/testing/bashdb/ 19: echo "i=$i result=$result"

To get around the strange initial next requirement I used the watche command in the below session, which lets you stop whenever an expression becomes true. In this case I'm not overly interested in the first few Fibonacci numbers so I set a watch to have execution stop when the result is greater than 4. You can also use a watche command without a condition; for example, watche result would stop execution whenever the result variable changed.

$ bash --debugger ./ (/home/ben/testing/bashdb/ 3: version="0.01"; bashdb<0> watche result > 4 0: (result > 4)==0 arith: 1 bashdb<1> continue i=1 result=1 i=2 result=1 i=3 result=2 i=4 result=3 Watchpoint 0: result > 4 changed: old value: '0' new value: '1' (/home/ben/testing/bashdb/ 19: echo "i=$i result=$result"

When a shell script goes wrong, many folks use the time-tested method of incrementally adding in echo or printf statements to look for invalid values or code paths that are never reached. With bashdb, you can save yourself time by just adding a few watches on variables or setting a few breakpoints.

Ben Martin has been working on filesystems for more than 10 years. He completed his Ph.D. and now offers consulting services focused on libferris, filesystems, and search solutions.

Share    Print    Comments   


on Debug your shell scripts with bashdb

Note: Comments are owned by the poster. We are not responsible for their content.

Debug your shell scripts with bashdb

Posted by: Anonymous [ip:] on November 24, 2008 10:28 PM
bashdb 4.0 has been tested on bash 4.0 alpha as well as bash 3.2.x.

There are also similar debuggers for zsh and ksh; these are called zshdb and kshdb respectively. But in contrast to bashdb, both require fairly new releases (ksh93t from Nov 4 or zshdb 4.3.6-dev-0 or greater) .

As with bashdb, zshdb in available as a source package in Debian. To get releases from sources though go to the same place:

Or if you prefer from git: and


Debug your shell scripts with bashdb

Posted by: Anonymous [ip:] on November 25, 2008 05:19 AM
Thanks, I'll bookmark this page for the next scripting session. It is just the quickstart needed by those of us who script only occasionally. Adding and removing those echo statements get tiring.


Debug your shell scripts with bashdb

Posted by: Jeenu V on November 25, 2008 06:34 AM
I can't get bashdb to run with bash --debugger ... I get:

BASH_EXECUTION_STRING='set|grep -i dbg'

But I can run bashdb directly. Am I supposed to (un)set any bash options for this to run?


Debug your shell scripts with bashdb

Posted by: Florian N on November 25, 2008 08:49 PM
You can simply check any bash script by using bash itself as a debugger and you can use this by using the "-n" string eg.:

bash -n


Debug your shell scripts with bashdb

Posted by: Anonymous [ip:] on November 26, 2008 07:10 AM
Your timing is impeccable! thanks.


Debug your shell scripts with bashdb

Posted by: Anonymous [ip:] on November 27, 2008 04:37 PM
Can i debug sh based shell scripts with bashdb.
Or is there a separate shdb like debugger for debuggung sh scripts.


Re: Debug your shell scripts with bashdb

Posted by: Anonymous [ip:] on November 29, 2008 06:00 AM
Bash is a superset of sh, so yes, it will work.


This story has been archived. Comments can no longer be posted.

Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya