Debugging mode – disabling the shell (option -n)

In the Bash shell, the -n option is a shortcut for noexec (as in no execution). This option tells the shell not to run the commands. Instead, the shell just checks for syntax errors.

We can test the script as follows:

    $ bash -n hello.sh
  

The -n option will tell the Bash shell to check the syntax in the Shell script but not to execute the Shell script.

Another way to do this is as follows:

#!/bin/bash  -n 
We have modified shebang line. 

In this case, we can test the Shell script as follows:

    $ chmod u+x hello.sh
    $ ./hello.sh
  

This option is safe, since the shell commands are not executed. We can catch incomplete if, for, while, case, and similar programming constructs as well as many more syntactical ...

Get Learning Linux Shell Scripting - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.