Debugging Linux Bash Scripts – POFTUT

Debugging Linux Bash Scripts

[rps-include post=6835]

While writing bash scripts we generally expects some results and provide these results to other scripts. But things goes not well as we expect. So we need to see under the hood and what is happening while script is running. Debugging is inspecting bash scripts to see what is wrong with script or show detailed information about script run.

Debugging Whole Script

To make whole script debug mode use -x parameter when calling the script

Our script is simple just issue host name -I command. As we can see that issued command is echoed to the console with +sign.

Debugging Some Part of Script

Debugging is useful even we have no problem with the script. It can give information about execution of the script. For long and big script files it may become hard to look debug. Some part of the bash script can be made debug-able like below.

This is our script where we set on debug with set -x and then set off with set +x.

After activating debug we can see the script command but after disabling debugging we can only see script output and cannot see script commands.

LEARN MORE  How To Use Linux dmesg Command To Print System Logs?

[rps-include post=6835]

1 Response

  1. John says:

    It’s a brilliant tutorial. Easy to understand and it’s Newbie-Friendly. I read this tutorial for a whole night and practiced for BASH again and again. I feel I can really do some jobs with BASH now. Thanks for İsmail Baydan, very nice tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)