Is it possible to check a bash script syntax without executing it?
Using Perl, I can run perl -c 'script name'
. Is there any equivalent command for bash scripts?
Is it possible to check a bash script syntax without executing it?
Using Perl, I can run perl -c 'script name'
. Is there any equivalent command for bash scripts?
I also enable the 'u' option on every bash script I write in order to do some extra checking:
This will report the usage of uninitialized variables, like in the following script 'check_init.sh'
Running the script :
Will report the following :
Very useful to catch typos
Perhaps an obvious caveat: this validates syntax but won't check if your bash script tries to execute a command that isn't in your path, like
ech hello
instead ofecho hello
.There is BashSupport plugin for IntelliJ IDEA which checks the syntax.
Time changes everything. Here is a web site which provide online syntax checking for shell script.
I found it is very powerful detecting common errors.
About ShellCheck
ShellCheck is a static analysis and linting tool for sh/bash scripts. It's mainly focused on handling typical beginner and intermediate level syntax errors and pitfalls where the shell just gives a cryptic error message or strange behavior, but it also reports on a few more advanced issues where corner cases can cause delayed failures.
Haskell source code is available on GitHub!
If you need in a variable the validity of all the files in a directory (git pre-commit hook, build lint script), you can catch the stderr output of the "sh -n" or "bash -n" commands (see other answers) in a variable, and have a "if/else" based on that
Change "sh" with "bash" depending on your needs
null command [colon] also useful when debugging to see variable's value