Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I syntax check a Bash script without running it?

People also ask

How do I test a bash file?

In Bash, you can use the test command to check whether a file exists and determine the type of the file. If you want your script to be portable, you should prefer using the old test [ command, which is available on all POSIX shells.

How can I tell the syntax of a shell script without running it?

Syntax Checking Using noexec Mode There can be situations where we may want to validate the script syntactically prior to its execution. If so, we can invoke the noexec mode using the -n option. As a result, Bash will read the commands but not execute them.

How do you validate the syntax of a Linux Bash script before running it?

Using Bash To Check Script Syntax. The Bash -n (noexec) option tells Bash to read a script and check it for syntactical errors, without running the script. Depending on what your script is intended to do, this can be a lot safer than running it and looking for problems.


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.


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.

enter image description here

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!


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


sh  -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.


I actually check all bash scripts in current dir for syntax errors WITHOUT running them using find tool:

Example:

find . -name '*.sh' -print0 | xargs -0 -P"$(nproc)" -I{} bash -n "{}"

If you want to use it for a single file, just edit the wildcard with the name of the file.


null command [colon] also useful when debugging to see variable's value

set -x
for i in {1..10}; do
    let i=i+1
    : i=$i
done
set - 

There is BashSupport plugin for IntelliJ IDEA which checks the syntax.