Debugging functions

So here I have a simple function that I wish to debug. However, I am unable to debug the desired function even with set -o functrace enabled. Before resorting to asking this question, I had managed to find a possible solution that did not produce the desired results, which is located here.

How can I get bash debug my functions?

So here I have a simple function that I wish to debug. However, I am unable to debug the desired function even with set -o functrace enabled. Before resorting to asking this question, I had managed to find a possible solution that did not produce the desired results, which is located here.

How can I get bash debug my functions?

#!/bin/bash
echo "Hello World"
hello() {
    echo "Hello world"
}

output:

user@mac11:53:29~/desktop bash -x debug.sh 
+ echo 'Hello World'
Hello World
user@mac11:54:55~/desktop 

Your function would be debugged - if you ever called it.

$ cat myScript
set -x
echo "Hello World"
hello() {
    echo "Hello world"
}

hello


$ ./myScript
+ echo 'Hello World'
Hello World
+ hello
+ echo 'Hello world'
Hello world
1 Like

Is debug.sh a built in function?
If yes, how to use it?

No, debug.sh is the name of BrandonD's shell script. The contents of debug.sh is shown in the 1st CODE tagged section of the 1st message in this thread. :wink:

I suppose it would be wise to call it, but in some scripts that I've seen do call the function. That's probably where I picked up that practice from. Is there any behavioral difference if a function is called or not?

If a function is never called, then it should be deleted, since it just fills up the space :slight_smile: