Debugging
Checking the syntax of a script with “-n”
Section titled “Checking the syntax of a script with “-n””The -n flag enables you to check the syntax of a script without having to execute it:
~> $ bash -n testscript.shtestscript.sh: line 128: unexpected EOF while looking for matching `"'testscript.sh: line 130: syntax error: unexpected end of fileDebugging usigh bashdb
Section titled “Debugging usigh bashdb”Bashdb is a utility that is similar to gdb, in that you can do things like set breakpoints at a line or at a function, print content of variables, you can restart script execution and more.
You can normally install it via your package manager, for example on Fedora:
sudo dnf install bashdbOr get it from the homepage. Then you can run it with your script as a paramater:
bashdb <YOUR SCRIPT>Here are a few commands to get you started:
l - show local lines, press l again to scroll downs - step to next lineprint $VAR - echo out content of variablerestart - reruns bashscript, it re-loads it prior to execution.eval - evaluate some custom command, ex: eval echo hi
b <line num> set breakpoint on some linec - continue till some breakpointi b - info on break pointsd <line #> - delete breakpoint at line #
shell - launch a sub-shell in the middle of execution, this is handy for manipulating variablesFor more information, I recommend consulting the manual: http://www.rodericksmith.plus.com/outlines/manuals/bashdbOutline.html
See also homepage:
http://bashdb.sourceforge.net/
Debugging a bash script with “-x”
Section titled “Debugging a bash script with “-x””Use “-x” to enable debug output of executed lines. It can be run on an entire session or script, or enabled programmatically within a script.
Run a script with debug output enabled:
$ bash -x myscript.shOr
$ bash --debug myscript.shTurn on debugging within a bash script. It may optionally be turned back on, though debug output is automatically reset when the script exits.
#!/bin/bashset -x # Enable debugging# some code hereset +x # Disable debugging output.