10

Is there some option I can give to just check the syntax of a [bash] shell script to check the syntax of it, but not actually execute anything nor cause any potential damage?

Daniel
  • 1,207
  • 3
  • 14
  • 30

2 Answers2

7

From the bash(1) man page:

-n      Read commands but do not execute them. This may be used to check a
        shell script for syntax errors. This is ignored by interactive shells.
Michael Mrozek
  • 91,316
  • 38
  • 238
  • 232
  • Obviously. I don’t know why I was so blind that I couldn’t see this when I was through the manual pages. – Daniel May 25 '11 at 21:34
1

Try out http://www.shellcheck.net

$ shellcheck myscript.sh

    In myscript.sh line 590:
    for f in $*; do
    ^-- SC1009: The mentioned parser error was in this for loop.


    In myscript.sh line 617:
        if [ ! -e "$somefile".vcf ]; then
        ^-- SC1046: Couldn't find 'fi' for this 'if'.
        ^-- SC1073: Couldn't parse this if expression.


    In myscript.sh line 1026:
    done
    ^-- SC1047: Expected 'fi' matching previously mentioned 'if'.
        ^-- SC1072: Unexpected keyword/token. Fix any mentioned problems and try again.

Well, it did not tell me the 'if' was missing on line 634 but it was pretty helpful.

Martin
  • 11
  • 1
  • It's also worth noting that you can download and run it locally, it runs in Haskell. – phk Apr 14 '17 at 19:17