Debugging bash scripts on command line is complicated.
Is there a good GUI based debugger available for debugging bash scripts?
The debugging options available in the Bash shell can be switched on and off in multiple ways. Within scripts, we can either use the set command or add an option to the shebang line. However, another approach is to explicitly specify the debugging options in the command-line while executing the script.
Use of set builtin command Bash shell offers debugging options which can be turn on or off using the set command: set -x : Display commands and their arguments as they are executed.
In fact, you are able to include GUI (Graphical User Interface) based input/output components into your next bash script using the Zenity command line tool which helps us to display GTK dialog boxes.
Download ShellEd eclipse plugin (update site didn't work for me) which allows editing bash scripts. Install this via Help -> Install new software -> Add -> Archive
.
Download basheclipse and extract the contents to your eclipse plugins
directory plugin.
Restart eclipse. Create a new Eclipse project BashTest
.
Add a file myscript.sh
to the project:
#! /bin/bash
. _DEBUG.sh
echo 'kshitiz'
echo 'This is a test'
x=1
y=3
z=3
Add _DEBUG.sh
to your project (Its in the downloaded zip for basheclipse
).
Go to Run -> Debug configurations
and create a new configuration under Bash script
category. Select myscript.sh
.
Then click Debug
. Open Debug
perspective.
Go to Window -> Preferences -> Shell script -> Interpreters
and ensure that interpreter is /bin/bash
. For me the default was /bin/dash
.
Set a breakpoint in the script. For some reason the right click menu does not show the option to set the breakpoint but Ctrl+Shift+B
should work.
Run the script Run -> Run as -> Run shell script
. The breakpoint will hit but it won't be able to find your script source (wierd eh!). Just click Select Bash script
and select myscript.sh
.
Now you can step through the code and analyze the variables:
A useful trick is to start your (executable) bash script with
#!/bin/bash -vx
at least during the debugging phase; you'll get a lot of helpful messages from bash. See bash(1)
If your script is not supposed to be used in a terminal (e.g. it is invoked from a crontab(5) entry) you could also use the logger(1) command.
At last, I believe that bash
is not the right tool to do complex scripting. If your script is becoming complex or bigger than a few dozens of lines, consider switching to a better scripting language (like GNU guile, python, gawk, etc...). In general, you should not code a lot in bash
. Having pain to debug a bash script is usually a symptom that you should recode that script in a better language.
You may also find useful to generate your shell script. autogen could be useful for that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With