Debugging bash scripts.


I'm looking to have some kind of debugger to debug bash scripts.

bashdb might be suitable but why isn't it in the usual package repositories?




Re: Debugging bash scripts.

By Oliver Grawert at 02/16/2019 - 17:43

Am Samstag, den 16.02.2019, 15:33 +0000 schrieb Ian Bruntlett:
by adding that line to the script, the shell itself pretty much acts
like a debugger, showing you where execution fails and what is going on
etc ...

another good hint is to use the shellcheck (sudo apt install
shellcheck) command on all your scripts, this will help with quoting
errors and help you to improve your syntax in general by making useful
suggestions about your code.


Re: Debugging bash scripts.

By oxy via ubuntu-users at 02/16/2019 - 18:10

On Sat, 2019-02-16 at 22:43 +0100, Oliver Grawert wrote:
Indeed, "#!/bin/ [...]" at the beginning of a script allows to use kinda
"debugging" options, too.


#!/bin/bash -ex

could be used instead of


set -ex

I just wonder how this could be useful, if it's intended that a command
could either finish with exit status 0 or another status. Actually a
script is a set of rules to interact regarding different exit statuses
("statuses" wow, an English plural that hurts as hell).

Debugging bash scripts.

By oxy via ubuntu-users at 02/16/2019 - 18:38

On Sat, 2019-02-16 at 23:10 +0100, Ralf Mardorf wrote:
Don't worry, while the English language has got some painful plurals,
the German language comes with some completely "off" translations of Old
Greek or Latin vocabulary, for example "Suizid/Suicidium" is "suicide"
in English, but the German words are usually "Selbstmord, Freitod etc."
all containing a value judgment already by the German word, something
that hurts way more, than obscure plurals.

Re: Debugging bash scripts.

By Liam Proven at 02/18/2019 - 06:45

On Sat, 16 Feb 2019 at 23:40, Ralf Mardorf via ubuntu-users
<ubuntu- ... at lists dot> wrote:
Are you talking to yourself? :-)

I would use something like "different exit status values" to avoid
trying to pluralise "status". ;-)

Re: Debugging bash scripts.

By Peter Flynn at 02/18/2019 - 09:52

On 18/02/2019 10:45, Liam Proven wrote:
I don't worry about it. The plural of status in English is statuses --
the word has been in the English language for over 200 years, so it has
lost its Latin behaviour.


Re: Debugging bash scripts.

By Dave Stevens at 02/16/2019 - 17:56

On Sat, 16 Feb 2019 22:43:45 +0100

great hints oli, thanks!


Re: Debugging bash scripts.

By oxy via ubuntu-users at 02/16/2019 - 12:33

Possible correct syntax, but bad typos could be work around by
"interactive" options, e.g.

It should read

sudo rm -r /tmp/foo

but regarding a typo it's

sudo rm -r / tmp/foo

to avoid serious trouble

sudo rm -Ir /tmp/foo

does the trick, even with a typo

sudo rm -Ir / tmp/foo

isn't executed without interaction.

mv -i foo bar

even doesn't require interaction, as long as "bar" doesn't exist.

If your bash scripts really need debugging that much, you not only
should consider to use a debugger, but also to run them in a sandboxed
environment (chroot, systemd-nspawn, virtual machine or fake-whatsoever
"development" container/install). Running the script without a debugger
anyway displays syntax errors:

[rocketmouse@archlinux tmp]$ cat test.bash


[rocketmouse@archlinux tmp]$ bash -n test.bash
[rocketmouse@archlinux tmp]$ bash test.bash
test.bash: line 3: pups: command not found

Btw. my apologies, "--debugger" requires bashdb:

[rocketmouse@archlinux tmp]$ bash --debugger test.bash
test.bash: /usr/share/bashdb/ No such file or directory
test.bash: warning: cannot start debugger; debugging mode disabled
test.bash: line 3: pups: command not found

Re: Debugging bash scripts.

By oxy via ubuntu-users at 02/16/2019 - 14:07

On Sat, 16 Feb 2019 17:33:16 +0100, Ralf Mardorf wrote:
My apologies for the amount of replies. A last one.

It's not meant as a disdainful comment. IMO using a debugger to debug
shell scripts is uncomfortable, since quite useless, since a bash script
is "just" a set of rules to run programs. Apart from syntax errors and
typos and errors in reasoning there is nothing that could go wrong. IOW
there is not much to debug and/or strace, STDERR/STDOUT is all we
need to check a shell script. Sometimes it might be required to strace
(or even to debug) a program used by the script, but not the script


You could try to build it yourself,
<a href="" title=""></a>, an Arch Linux package
seems to be doable by just running

./configure --prefix=/usr --disable-static -C

and apart from a basic Arch install it requires
'bash', 'python-pygments' and 'pygmentize' packages (for Ubuntu probably
dev packages ;).

On Ubuntu I would finish by running

sudo checkinstall

instead of "sudo make install",
<a href="" title=""></a>.

Re: Debugging bash scripts.

By Liam Proven at 02/18/2019 - 06:49

On Sat, 16 Feb 2019 at 19:10, Ralf Mardorf via ubuntu-users

I think that is not fair or accurate.

I have not looked into the question of whether Bash is
Turing-complete, but it is a proper programming language, with
variables, branches (conditional execution), and looping.

As such, it's as prone to bugs as any other language.

The point I might agree with you on is that, in my very limited
experience, the use of a separate debugger tool is something mainly
used with compiled languages, where you are trying to work out what is
going wrong with a binary executable. A sort of "black box".

In interpreted languages, debugging can consist of pausing the
program, looking at variable values, maybe modifying them, resuming,
jumping to a different execution point, test-running fragments of
code, etc. All this is hard or impossible to do in compiled languages.
That is why they have separate debuggers.

Re: Debugging bash scripts.

By Mike Marchywka at 02/18/2019 - 06:59

From what I have observed, it is used different than say javascript
for which browsers have debuggers and it is not similar to say BASIC.
I guess personally just longer error messages would help with bash per
se but usually these are easy to fix when you find them.
The way bash is normally used you may end up debugging the programs
it invokes more than bash syntax. For that and learning I just
end up dumping everything and making methods for that basics
so you can go into debug mode with a few conditionals.

The condescending c++ errors mesages can be very helpful :)


note new address
Mike Marchywka 306 Charles Cox Drive Canton, GA 30115
2295 Collinworth Drive Marietta GA 30062. formerly 487 Salem Woods Drive Marietta GA 30067 404-788-1216 (C)<- leave message 989-348-4796 (P)<- emergency

Re: Debugging bash scripts.

By oxy via ubuntu-users at 02/16/2019 - 14:32

On Sat, 16 Feb 2019 19:07:50 +0100, Ralf Mardorf wrote:
Oops, another last message :D.

I could imagine that a debugger could be useful to fix race conditions,
but actually I don't remember any such complex race condition, that I
ever wished to have a debugger.

I remember that an old script needed a loop

$ cat /usr/local/sbin/alice | grep pppoe_off\( -A4
pppoe_off() {
echo; poff -a; ip link set enp3s0 down; printf "Progress: "
while pidof pppd > /dev/null; do printf "."; sleep 3; done;
echo; modprobe -vr pppoe; echo

but it's not complex and STDERR of modprobe most likely informed me about
the issue.

Re: Debugging bash scripts.

By Ian Bruntlett at 02/16/2019 - 15:11

Hi Ralf,

Thanks for your replies. I'm looking for a Bash debugger because I'm
working my way through "The Linux Command Line" (downloadable as a PDF from
<a href="" title=""></a> - I have the printed version) and I am
being thorough and want to use Bash and a debugger to gain fluency in bash.



Re: Debugging bash scripts.

By Mike Marchywka at 02/16/2019 - 16:21

As an interpretter it is a bit different from debugging compiled languages.
You also have the added flexibility in that optimization and speed are almost never
a factor- otherwise you would compile and the code is almost always concise.

I just installed 'Beaver on a USB stick and cleaned up a lot of "junk directories" where I did not
bother to separate documents or projects. In the process I ended up with some scripts
that I named things like "setup" that reside in each project directory and may invoke a chain
higher up- ../setup calls ../setup etc and some global setup that just needs to be on the path and
defines global variable like places for scripts to put data. Also use scripts as documentation
for things like latex search path variables.
I also ahve scripts that go get data from unstructured web pages- trying to take a link to
an article and get the bibtex for that article so I can reference it in my own work. This can
involve scraping the link but often just comes down to getting a doi and calling crossref.
Between the page scaping and network there is a lot that can go wrong and can be
hard to debug. .

In that process I just found that syntax errors come up much later as it is hard
to get good coverage like a compiler may get. But also if you read things like config scripts
you can see that just about anything can vary locally and you can get some usages ideas from things like
that. But anyway my point was for debugging I just finally made methods for everything-
so for example none of the scripts will call "wget" but rather have variables with values like
"/foo/wget -q" or "/foo/wget -S -v" depending on how debug flags are set.
This let's you easily debug for learning and dealing with unexpected web events.

The great thing is that when you are done you have tractable documentation
on what you have done and what needs to be done. This differs from point and click
on things.


note new address
Mike Marchywka 306 Charles Cox Drive Canton, GA 30115
2295 Collinworth Drive Marietta GA 30062. formerly 487 Salem Woods Drive Marietta GA 30067 404-788-1216 (C)<- leave message 989-348-4796 (P)<- emergency

Re: Debugging bash scripts.

By oxy via ubuntu-users at 02/16/2019 - 17:31

On Sat, 16 Feb 2019 20:21:57 +0000, Mike Marchywka wrote:
Indeed "verbose" options are more useful than "quite" options are and
there are pitfalls when using "&&" (or "||"), since they not
necessarily do the job, let alone "&", regarding race
conditions, especially, if shell external commands and more especially,
if special command, such as web related commands are used.

For example

man bash | grep wget

returns absolutely nothing.

Actually scripts are not a programming language. Are scripts a
markup or interpretor languaben and what does it all mean related to a
debugger? ;)!

Re: Debugging bash scripts.

By oxy via ubuntu-users at 02/16/2019 - 17:53

On Sat, 16 Feb 2019 22:31:29 +0100, Ralf Mardorf wrote:
Again, a script is a set of conditions to run programs, a script is not
a program. For my understanding it is reasonable to use a debugger to
check a program, but I don't understand how a debugger could be useful
to check a script.

JFTR if a log file should be wanted


is standard Bourne/POSIX shell

it's said that


does the job when using bash, I never tested it, actually redirecting


works with bash, too.

Re: Debugging bash scripts.

By oxy via ubuntu-users at 02/16/2019 - 16:02

On Sat, 2019-02-16 at 19:11 +0000, Ian Bruntlett wrote:
Well that's fair enough :)!

I'm an Assembly language dino who migrated from Commodore 64 and Atari
ST (+ 80286 hardware emulator) to Linux, so regarding to my background I
even prefer the trial-and-error principle and some googling over even
reading man bash and man dash, let alone the guide you mentioned or
similar guides. Actually most of my older scripts would have benefit
from reading the man pages in the first place :D. At least the 'shift'
command did render tons of lines of my old script needless. However,
there are some common used guides available. I was stupid and mostly
ignored them, so I actually don't remember the links. IIRC the common
recommended guides might be
<a href="" title=""></a> and
<a href="" title=""></a>, but I might be mistaken, somebody
else should chime in.

A debugger for shell scripts IMO still seems to be inappropriate, but
it's possible that I miss the benefits of such a debugger, OTOH
seemingly neither Ubuntu and Debian, nor Arch provide it by official
repositories. At least for Arch I found a user repository providing
<a href="" title=""></a>, "Description: A debugger for
Bash scripts loosely modeled on the gdb command syntax".

While I'm nowadays able to understand the "--help" and "man pages"
syntax, I remeber that it was way to hard to understand when I started,
so Google and guides are good in combination with the trial-and-error

While I prefer other search engines over Google, Google most of the
times provide better search results, if we are unaware about the
smartest search terms ;).

I wonder if somebody on this list does use a debugger for bash scripts.


PS: I try to write scripts that are at lest bash and dash compatible. As
the log in shell I prefer bash, because I use it for almost everything
directory/file and package management related on Ubuntu and Arch Linux,
but to run fast scripts, I prefer dash over bash, when ever possible. In
short, dash is faster, but so called 'bashisms' sometimes are more
comfortable to use. Dash is more portable than bash, assuming you want
to take a look at other POSIX/UNIXoid operating systems. However, bash
without doubts is used as the most common login shell for Linux distros,
so it's wise to care about bash.

I still doubt that the debugger is useful, but perhaps the time comes
you are able to prove me wrong :).

Re: Debugging bash scripts.

By oxy via ubuntu-users at 02/16/2019 - 12:36

On Sat, 16 Feb 2019 17:33:16 +0100, Ralf Mardorf wrote:
in a safe environment

as well as typos ;).

Re: Debugging bash scripts.

By oxy via ubuntu-users at 02/16/2019 - 11:58

On Sat, 16 Feb 2019 15:33:43 +0000, Ian Bruntlett wrote:
[weremouse@moonstudio ~]$ bash -c "help set" | grep '\-n'
set: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
-n Read commands but do not execute them.
-o option-name
Set the variable corresponding to option-name:
noexec same as -n
[weremouse@moonstudio ~]$ man bash | grep debugger
Arrange for the debugger profile to be executed before
the shell starts. Turns on extended debugging mode (see the
description of the extdebug option to the shopt builtin If set,
behavior intended for use by debuggers is enabled:

However, running

bash -n your_script

checks the syntax but can't detect issues regarding some typos.
IOW if you typed to execute "sl" instead of ls" and "ls" shouldn't be
provided by PATH or it should be by
<a href="" title=""></a>, the "-n" option doesn't help.

I can't say anything about the "--debugger" option, actually I neither
use "-n", nor "--debugger" to check my bash scripts.

Re: Debugging bash scripts.

By oxy via ubuntu-users at 02/16/2019 - 12:11

On Sat, 16 Feb 2019 16:58:38 +0100, Ralf Mardorf wrote: