better bash debugging

2011-09-16 161 words 1 min read

I was trying to debug some bash scripts yesterday and learnt something new πŸ˜‰
There are lot of things in bash that you can use to debug and to start with you can enable xtrace mode as follows:

set -x

With this set you will see that all the commands are printed along with all other information as they are executed.
This you can do for any line or for the function or for the whole script. Once you set the option you can turn it
off with

set +x

Now, once you have enabled this, you will see a lot of output, but you will still not see the function name and the line numbers. If you do want to see them then you will need to set the PS4 variable like below and bingo :

export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
Enhanced by Zemanta


Authored By Amit Agarwal

Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.

We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now
This website uses cookies to ensure you get the best experience on our website. Learn more Got it