0

I am wondering how I can validate user input, to check if it is a valid bash command - in terms of syntax? I guess it is not possible to create regex for that. Are there any libraries to handle this?

Thanks

6
  • Just show actual error(stderr) if command failed, while execution. Commented Jun 9, 2016 at 6:13
  • The issue is, that I want to validate on the front end. Commented Jun 9, 2016 at 6:13
  • Almost all character sequences can be interpreted as valid bash command Commented Jun 9, 2016 at 6:14
  • 1
    Valid related to what? It can be valid on one system and invalid on another. Is it valid when it produces an error output? Commented Jun 9, 2016 at 6:17
  • @GünterZöchbauer, valid in terms of a syntax. I will correct it in the question, to make it clearer. Commented Jun 9, 2016 at 6:20

1 Answer 1

3

You can't. There is no practical way to validate a shell command without running it.

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

2 Comments

thanks for your answer. I will leave the question open for couple of hours, and if nothing pops out, I will mark it as correct.
This is right. A shell command can only be valid according to the shell itself. Another layer cannot tell if it will be valid in that shell. The best you can do is to inspire you from most common keywords. Still, it will be a poor approximation of what the shell can and cannot understand.

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.