7

I have a bash script myscript.sh, which has a bunch of functions inside. To run one function, I would write

source myscript.sh; myfunction

How debug such a function?

1

3 Answers 3

6

Just add these line before section you want to debug.

set -v -x -e

and to disable it.

set +v +x +e

3

enable debugging in your script and output debugging for functions with functrace.

set -x
set -o functrace
2
  • Is there any way to direct the output this produces to a log file? Commented Apr 16, 2023 at 18:02
  • @BrittonKerin yourScript.sh 2>1& >output_to_file. Commented Apr 17, 2023 at 5:16
1

You should also in Bash be able to use:

$ set -xT

which enables function trace and

$ set +xT

to disable.

Also, with reference to another posted answer, I don't recommend using -v for debugging (verbose mode) unless you are checking a script/set of shell commands for syntax. One other (general) solution that is sometimes useful is making use of the -n option in conjunction with -x. This will "run" a script/commands without actually doing the last step of running a command. This can often let you pick up difficult errors in shell expansions and the like without causing unwanted mayhem.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .