1

I'm looking for a way to validate the syntax of a bash script without executing it.

bash -n only seems to validate base bash syntax, what I am missing is:

  1. function name validations
  2. un-initialized parameter validations

Any ideas on how to achieve that?

I am also missing the validation of the number of parameters a function takes, but that sounds like a very hard thing to do in theory, in bash.

In other words what I'd like to do is pretty much take a bash script and "compile" it like I would compile a c++ program.

4
  • 1
    May I ask what the motivation is? That might help us provide the best solution. Commented Feb 17, 2014 at 13:42
  • shellcheck.net Commented Feb 17, 2014 at 13:42
  • I'm familiar with spellcheck. That is not good enough for my need. I'd like to provide developers with API, and than "compile" their code prior to shipping the product... That's my motivation for checking the code before run-time. Ideas? Commented Feb 18, 2014 at 13:58
  • 1
    You can run download and compile shellcheck to run locally. Would that help? Beyond that, running bash -n, or using set -x there aren't a lot of options for shell scripts. There are some options for doing a conversion to binary, though I think what you're looking for is a build system. There are some frameworks for doing unit testing; I've compiled a list of those if that's helpful. And it looks like someone has built a build system as well if you'd like that info. Commented Feb 18, 2014 at 17:13

1 Answer 1

3

bash -n is certainly the fundamental way to do that.

However there is also a site that will do some validation for you: http://www.shellcheck.net/ --And there is a link on the site to the source code if you want to run it locally.

Sign up to request clarification or add additional context in comments.

Comments

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.