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?

7
  • 3
    As far as bash knows, there may be a command by the name of eco, so this isn't really a syntax error. Commented Nov 27, 2014 at 14:47
  • OK I see. If you answer and put a bad example I will accept. Thank you. Commented Nov 27, 2014 at 14:50
  • possible dup:stackoverflow.com/questions/171924/… Commented Nov 27, 2014 at 14:50
  • I know there are others but they are not specific enough probably. Commented Nov 27, 2014 at 14:50
  • 1
    dvd818 showed that there is a website you can use: shellcheck.net Commented Nov 27, 2014 at 14:51

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

1 Comment

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.