How do I syntax check a Bash script without runnin

2020-01-25 03:17发布

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?

8条回答
▲ chillily
2楼-- · 2020-01-25 03:58

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

Example:

find . -name '*.sh' -exec bash -n {} \;

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

查看更多
smile是对你的礼貌
3楼-- · 2020-01-25 03:59
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.

查看更多
登录 后发表回答