better bash debugging

2011-09-16 1 min read bash Linux

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
comments powered by Disqus