How to debug bash script?

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


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!


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

Tags:

Debugging

Bash