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?
bash -n scriptname
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 of echo hello.
set does.if ["$var" == "string" ] instead of if [ "$var" == "string" ]type [ says "[ is a shell builtin". It ultimately delegates to the test program, but it expects a closing bracket, as well. So it's like if test"$var", which isn't what the author meant, but is syntactically valid (say $var has a value of "a", then we will see "bash: testa: command not found"). Point is that syntactically, there is no missing space.[ is only invoked in this case if $var happens to expand to an empty string. If $var expands to a non-empty string, [ is concatenated with that string and is interpreted as a command name (not function name) by Bash, and, yes, that is syntactically valid, but, as you state, obviously not the intent. If you use [[ instead of [, even though [[ is a shell keyword (rather than a builtin), you'll get the same result, because the unintended string concatenation still overrides recognition of the keyword.["$var" is syntactically a valid command-name expression; similarly, tokens == and "$string" are valid command arguments. (Generally, builtin [ is parsed with command syntax, whereas [[ - as a shell keyword - is parsed differently.) The shell builtin [ does not delegate to the "test program" (external utility): bash, dash, ksh, zsh all have builtin versions of both [ and test, and they do not call their external-utility counterparts.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.

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!
apt-get install shellchecktrusty-backports.I also enable the 'u' option on every bash script I write in order to do some extra checking:
set -u
This will report the usage of uninitialized variables, like in the following script 'check_init.sh'
#!/bin/sh
set -u
message=hello
echo $mesage
Running the script :
$ check_init.sh
Will report the following :
./check_init.sh[4]: mesage: Parameter not set.
Very useful to catch typos
set -u although this does not really answer the question, because you have to run the script to get the error message. Not even bash -n check_init.sh shows that warningsh -n script-name
Run this. If there are any syntax errors in the script, then it returns the same error message.
If there are no errors, then it comes out without giving any message. You can check immediately by using echo $?, which will return 0 confirming successful without any mistake.
It worked for me well. I ran on Linux OS, Bash Shell.
sh -n will probably not check that the script is valid Bash script. It might give false negatives. sh is some Bourne shell variant that is usually not Bash. For example in Ubuntu Linux realpath -e $(command -v sh) gives /bin/dashFor only validating syntax:
shellcheck [programPath]
For running the program only if syntax passes, so debugging both syntax and execution:
shellproof [programPath]
There is BashSupport plugin for IntelliJ IDEA which checks the syntax.
.sh or other extension associated with Bash scripts, which is the case if you generate the scripts using some templating tool like ERB (then they end with .erb). Please vote for youtrack.jetbrains.com/issue/IDEA-79574 if you want it fixed!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
bashErrLines=$(find bin/ -type f -name '*.sh' -exec sh -n {} \; 2>&1 > /dev/null)
if [ "$bashErrLines" != "" ]; then
# at least one sh file in the bin dir has a syntax error
echo $bashErrLines;
exit;
fi
Change "sh" with "bash" depending on your needs