bash: debug option and functions bash: debug option and functions unix unix

bash: debug option and functions


With bash, you can use functrace option in your script

set -o functrace

See manpage for bash for other debugger options.


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

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

I run it with debug option turned off:

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

and turned on (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 fun01() function is showed with a starting + which is the debugger in action.

@bash-o-logist Even if I add variable or if/then/else conditional constructs I still get all the debug info:

[root@ ~]# cat debug-new.sh#!/bin/bashfun01 () {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 2fun01exit

Executing 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


In ksh use typeset -ft function-name to trace into a function