Hi, this is Sandra Henry-Stocker, author of the “Unix as a Second Language” blog on NetworkWorld.
In this Linux tip, we’re going to look at using the bash shell’s “debugging mode”. It can help you pinpoint problems, especially in long or complicated scripts.
One way to use debugging mode is to start a bash script like this:
Alternately, you can change the shebang line in the script to look like this:
That -x will make bash display each line in the script as it’s being run. This allows you to associate any errors you see with the command that caused them.
For example:
In this case, we’re running into an error because the script expects 2 arguments and exits if they aren’t provided. In this next run, the script exits because the date command in the 8th line of output shown (date +%b) provides just a month name when the script is trying to match a month and day. Afterwards, it looks for a file that doesn’t exist.
Lines starting with + signs are displayed because we’re using debugging mode. The others are the normal script output.
That’s your Linux tip for bash debugging mode.
If you have questions or would like to suggest a topic, please add a comment below. And don’t forget to subscribe to the IDG Tech(talk) channel on YouTube.
If you liked this video, please hit the like and share buttons. For more Linux tips, be sure to follow us on Facebook, YouTube and NetworkWorld.com.