How do I Debug BASH Scripts?

Asked

Viewed 426 times

8

How can I Debug bash Scripts? No OF I can use @echo ON and @echo OFF. I would like to use something analogous on Linux.

1 answer

10

Use the following:

sh -x script [arg1 ...]
bash -x script [arg1 ...]

This allows to make a trace of what is being executed.

Another useful option is -n which means in Execution and -v which means verbose mode. It is useful to combine these parameters to further facilitate your bash shell DEBUG when you invoke by the prompt.

If you wish to control the trace from within Script in specific blocks of code use:

set -x

for connect the trace. To turn off the trace use:

set +x
  • 2

    @bmkrio, What can also help is the option set -v / +v , it displays the code before running without parse (original code).

  • A good help in debugging is using shellcheck.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.