Linux: Check the syntax of a script without executing it

If you update a script in a production environment and want to check whether the syntax is still correct without executing it, you can call it this way:

# sh -n myshellscript

or you can add the following at the beginning of the script:

set -n

This will have the shell read the script but not execute it. It will then output error messages for all syntax errors found e.g.:

# sh -n myshellscript
myshellscript: line 61: syntax error near unexpected token `else'
myshellscript: line 61: `   else'

Please note that an interactive shell might ignore set -n when executing commands interactively.

Leave a Reply

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