How to Perform Syntax Checking Debugging Mode in Shell Scripts

We started the shell script debugging series by explaining the different debugging options and how to enable shell script debugging modes.

After writing your shell scripts, it is recommended that we practically check the syntax in the scripts before running them, as opposed to looking at their output to confirm that they are working correctly.

In this part of the series, we will go through how to use syntax checking debugging mode. Remember we explained the different debugging options in the first part of this series and here, we will use them to perform script debugging.

Enabling Verbose Debugging Mode

Before we move to the primary focus of this guide, let us briefly explore the verbose mode. It is enabled by the -v debugging option, which tells the shell to display all lines in a script while they are read.

To demonstrate how this works, below is a sample shell script to batch convert PNG images to JPG format.

Type ( or copy and paste) it in a file.

#!/bin/bash
#convert
for image in *.png; do
        convert  "$image"  "${image%.png}.jpg"
        echo "image $image converted to ${image%.png}.jpg"
done
exit 0

Then save the file and make the script executable using the command below:

$ chmod +x script.sh

We can invoke the script and display all the lines in it as they are read by the shell like so:

$ bash -v script.sh
Display All Lines in Shell Script

Display All Lines in Shell Script

Enabling Syntax Checking Debugging Mode in Shell Scripts

Coming back to our topic of emphasis, The -n activates syntax checking mode. It instructs the shell to basically read all the commands, however doesn’t execute them, it (shell) only examines the syntax used.

In case there are errors in your shell script, the shell will output the errors on the terminal, otherwise, it displays nothing.

The syntax for activating syntax checking is as follows:

$ bash -n script.sh

Because the syntax in the script is correct, the command above will not display any output. Therefore, let us try to remove the done word that closes the for loop and see if it shows an error:

Below is the modified shell script to batch convert png images to jpg format that contains a bug.

#!/bin/bash
#script with a bug
#convert
for image in *.png; do
        convert  "$image"  "${image%.png}.jpg"
        echo "image $image converted to ${image%.png}.jpg"

exit 0

Save the file, then run it while performing syntax checking in it:

$ bash -n script.sh
Check Syntax in Shell Script

Check Syntax in Shell Script

From the output above, we can see that there is a syntax problem with our script, the for loop is missing a closing done keyword word. And the shell looked for it up to the end of the file and once it did not find it (done), the shell printed a syntax error:

script.sh: line 11: syntax error: unexpected end of file

We can as well combine the verbose mode and syntax checking mode together:

$ bash -vn script.sh
Enable Verbose and Syntax Checking in Script

Enable Verbose and Syntax Checking in Script

Alternatively, we can enable syntax checking by modifying the first line of the script above as in the next example.

#!/bin/bash -n
#altering the first line of a script to enable syntax checking

#convert
for image in *.png; do
    convert  "$image"  "${image%.png}.jpg"
    echo "image $image converted to ${image%.png}.jpg"

exit 0

As before, save the file and run it while performing syntax checking:

$ ./script.sh

script.sh: line 12: syntax error: unexpected end of file

In addition, we can employ the set shell built-in command to enable debugging mode in the script above.

In the example below, we are only checking the syntax of the for loop in our script.

#!/bin/bash
#using set shell built-in command to enable debugging
#convert

#enable debugging
set -n
for image in *.png; do
    convert  "$image"  "${image%.png}.jpg"
    echo "image $image converted to ${image%.png}.jpg"

#disable debugging
set +n
exit 0

Once again, save the file and invoke the script:

$ ./script.sh 

In summary, we should always ensure that we syntactically check our shell scripts to capture any error before executing them.

To send us any questions or feedback concerning this guide, use the response form below. In the third part of this series, we shall move to explaining and using shell tracing debugging mode.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

4 Responses

  1. Arnau says:

    Another very useful option is set -x

    • Aaron Kili says:

      @Amau

      Yap, in the next part of the shell debugging series, we will cover shell tracing or shell execution tracing which is enabled by the -x option. So stay connected to read more about this option. And thanks for pointing that out.

  2. danofsatx says:

    If you’re running the script with bash, you don’t need to make it executable for debugging.

Got something to say? Join the discussion.

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.