Visit Jeremy's Blog.
Go Back > Blogs > linux-related notes
User Name


Just annotations of little "how to's", so I know I can find how to do something I've already done when I need to do it again, in case I don't remember anymore, which is not unlikely. Hopefully they can be useful to others, but I can't guarantee that it will work, or that it won't even make things worse.
Rate this Entry

Simple idea for a debug verbosity tool on bash scripts

Posted 09-07-2021 at 09:06 AM by the dsc
Updated 08-05-2023 at 08:37 PM by the dsc
Tags bash, debug

I used to just have lots of "echos" around my scripts printing where it is on a loop or whatever, so I have a better idea of whats happening, often somewhat more clear than with "bash -x", even though that also may help, of course. But the odds are that I'd at some point just leave such debug echos there while the script runs on a daily basis on the background, just helping .xsession-errors to become enormous, for no good reason, since it's working and I'm not reading it. And then it can take some time to go and comment all the needless echos and printfs, paying attention if they're really just debug messages.

A simple alternative is to have either a function or perhaps an auxiliary script at $PATH that would echo what's put in front of it "debug=1" or something. Maybe it can even be somewhat tweaked to have degrees of verbosity.


dbgecho() {
(( debug > 0 )) && echo $@


dbgecho "whatever man, debugmsg"
dbgecho -e "debug\ndebug\ndebug"


dbgecho "nah"

May be even some variable/string, I guess.

((debug > 0 )) && dbgecho="echo" || dbgecho=":"

# then you'd use it with the dollar sign, though:

$dbgecho "debug message"
But if it's the last thing the script runs, it would falsely give a non-error exit sign, if that matters. Well, it would be the case anyway with plain echo, so if one's using the exit signs it has to be less sloppily planned than something that would be affected by it.

For practical debug purposes it works almost identically except for the "$" part, but maybe one or the other requires some more CPU power, although that would be probably negligible in most cases, unless there's perhaps some full-throttle never-ending loop with lots of it to the point of making a difference, if that's possible.

Related line of ideas:


echo "${@}" >> ${logfile}
Maybe one can think of some verbosity toggle that would print either just to the log file or both to the log file and stdout, like maybe using "tee -a" instead of echo.
Posted in Uncategorized
Views 444 Comments 0
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 11:34 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration