Bash: option and debugging features

If I run

bash -x myscript.sh 

I get debug output.

But if I have a function in myscript.sh , the function code is immune to -x. It writes for output only the name of the function.

How to get debug output for functions in bash scripts?

Update:

After ztank1013's answer, I just realized that I used ksh, not bash. Bash seems to have enabled the functrace function on my system by default (thanks bash -o-logist)

I am satisfied, but for the community I keep the question open to ksh.

For the script:

 #!/bin/ksh a=2 testering(){ a=3 if [ $a -eq 3 ]; then echo lili fi } if [ $a -eq 2 ]; then echo mimi fi testering exit 

ksh -x ./testdebug.sh :

 + a=2 + [ 2 -eq 2 ] + echo mimi mimi + testering lili + exit 

So for ksh, what's the trick?

(If there is no answer, the "correct" will go to bash -o-logist.)

+7
source share
4 answers

With bash you can use the functrace option in a script

 set -o functrace 

See the man page for bash for other debugger options.

+8
source

I cannot reproduce your problem, in fact, given my test script (debug.sh):

 [root ~]# cat debug.sh #!/bin/bash fun01 () { echo "BUT HERE I am inside the function fun01() body" } echo "HERE I am outside the function fun01() body!" sleep 2 fun01 exit 

I run it with the debug option disabled:

 [root ~]# ./debug.sh HERE I am outside the function fun01() body! BUT HERE I am inside the function fun01() body 

and included (bash -x ...):

 [root ~]# bash -x ./debug.sh + echo 'HERE I am outside the function fun01() body!' HERE I am outside the function fun01() body! + sleep 2 + fun01 + echo 'BUT HERE I am inside the function fun01() body' BUT HERE I am inside the function fun01() body + exit 

As far as I can see, the line executed inside the function fun01 () is displayed with the beginning +, which is a debugger in action.

@bash -o-logist Even if I add variables or conditional constructs if / then / else, I still get all the debugging information:

 [ root@ ~]# cat debug-new.sh #!/bin/bash fun01 () { INSIDEVAR='Never really use this one' echo "BUT HERE I am inside the function fun01() body" if [ true ] ; then echo 'this is going to be printed always!' ; fi } echo "HERE I am outside the function fun01() body!" sleep 2 fun01 exit 

Running again:

 [ root@ ~]# bash -x debug-new.sh + echo 'HERE I am outside the function fun01() body!' HERE I am outside the function fun01() body! + sleep 2 + fun01 + INSIDEVAR='Never really use this one' + echo 'BUT HERE I am inside the function fun01() body' BUT HERE I am inside the function fun01() body + '[' true ']' + echo 'this is going to be printed always!' this is going to be printed always! + exit 
+4
source

In ksh use typeset -ft function-name to track in function

+2
source

I had a similar question and ended up writing my own debbuger for Bash. Try it ... ... I hope this helps you https://sourceforge.net/projects/bashdebugingbash/

0
source

All Articles