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

Tom Feiner picture Tom Feiner · Oct 5, 2008 · Viewed 220.6k times · Source

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?

Answer

andy picture andy · Oct 5, 2008
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.