Is it feasible to check the syntax of a bash script without running it?
I can use Perl to run perl -c’script name’. Is there a bash script equivalent command?
Asked by Tom Feiner
bash -n scriptname
This validates syntax, however it won’t verify if your bash script tries to run a command that isn’t in your path, such as ech hello instead of echo hello.
Answered by andy
Everything changes with the passage of time. A website that provides online syntax checking for shell scripts may be found here.
I discovered that it is really effective at spotting frequent problems.
ShellCheck is a sh/bash script static analysis and linting tool. It focuses on common beginner and intermediate level syntax problems and hazards, when the shell just displays a cryptic error message or behaves strangely, but it also highlights a few more complex concerns where corner situations might result in delayed failures.
The source code for Haskell is accessible on GitHub!
Answered by dvd818
In order to do some extra verification, I also enable the ‘u’ option in every bash script I write:
As with the script ‘check init.sh,’ this will report the use of uninitialized variables.
#!/bin/sh set -u message=hello echo $mesage
Execution of the script:
Will provide the following information:
It’s a great way to catch mistakes.
Answered by Diego Tercero
sh -n script-name
This should be run. If the script contains any syntax mistakes, the same error message is displayed. If there are no errors, it will exit without displaying a message. You may check right away by typing echo $?, which will return 0 indicating that everything went well.
It worked for me well. I ran on Linux OS, Bash Shell.
Answered by Jeevan
Without using the locate tool, I examine all bash scripts in the current directory for syntax errors:
xargs -0 -P”$(nproc)” -I bash -n “” find. -name ‘*.sh’ -print0 | xargs -0 -P”$(nproc)” -I bash -n “”
If you just want to utilize it for one file, simply replace the wildcard with the file’s name.
Answered by Gerald Hughes
Post is based on https://stackoverflow.com/questions/171924/how-do-i-syntax-check-a-bash-script-without-running-it