0

I need to check whether a set of scripts are syntactically correct. I know there are couple of posts around suggesting to use bash -n <script_name> but I run this and I get nothing out e.g.

good.sh

#!/bin/bash

echo "hello"

bad.sh

#!/bin1/bash1

eco "hello"

If I do:

$ bash -n bad.sh; echo $?
0
$ bash -n good.sh; echo $?
0

So how do you discriminate between good and bad exactly?

SkyWalker
  • 13,729
  • 18
  • 91
  • 187

1 Answers1

2

bash -n checks whether Bash can parse the code (that is, the syntax is correct), not whether the code is "correct." "Correct" can have a lot of meanings, most of which programs will never be able to verify:

  • Can be parsed (bash -n).
  • Finishes without error (if ./script.sh; then [...]; fi).
  • Prints something which follows a specific format.
  • Prints something useful.
  • Any of the above within a specific environment, for example one which has a shell interpreter that lives in /bin1/bash1 and a command eco which shows you the most ecologically friendly beer bottles available within a 5 parsec radius.
l0b0
  • 55,365
  • 30
  • 138
  • 223
  • It's too bad the `-e` option can't be overloaded with `-n` to indicate something like "check if command is available" (and continue to test for syntax errors). – shellter Nov 27 '14 at 16:27