How to debug bash script?

Is there any way to debug bash script without using echo and logging?

I'm talking about using breakpoints and stuff like that.


Solution 1:

There several different ways, i.e.:

  • Run your script with the -x option

    bash -x yourscript.sh
    
  • Add set -x in a new line at the beginning of your scriptfile after the Shebang #!/bin/bash and before the beginning of your script, set -v would display the shell input lines, set -x displays the executed commands and arguments

  • replace the shebang of your script file with #!/bin/bash -xv

Solution 2:

Yes, there is Bash Debugger Project.

You can also use set -x and set -v at execution. More info:

http://www.linuxtopia.org/online_books/advanced_bash_scripting_guide/debugging.html http://www.cyberciti.biz/tips/debugging-shell-script.html

Good luck!

Solution 3:

Nowadays, there is the Visual Studio Code Bash Debug extension. It has 'Step in/out/over' and can show the value of each variable.

VS Code Bash Debug screenshot