Hacker News new | ask | show | jobs
by HenrikB 1581 days ago
(Disclaimer: I'm the co-developer)

Together with GitHub user xPMo, I created a Shellcheck REPL tool (https://github.com/HenrikBengtsson/shellcheck-repl) that validates your Bash commands using ShellCheck _before_ they are evaluated. For example,

$ words="lorem ipsum dolor" $ echo $words ^-- SC2086: Double quote to prevent globbing and word splitting.

It was a toy project at first, but since I've learned so much about Bash from using it, I now have it enabled all the time.

3 comments

A very happy and grateful user here, jumping in here just to thank you for the time and efforts to create this one! It saved me many hours of debugging after stupid midnight-coding-sessions mistakes. We even have it in our coding-style internal guidelines for some time now.
Neat. I like it.

I do have one suggestion: you have it ignoring "SC2154: 'var' is referenced but not assigned" by default, which makes sense on the command line because you're often not assigning and then referencing the same variable in a single command. But, I think it would be useful to have a similar warning like "SCREPL01: 'var' is not defined in the local environment," or something, which you might implement in shellcheck-repl itself. That rule could simply check to see if 'var' exists in the current shell's environment or if it's defined in the user's current command.

I think bash is simple enough that you might not have to a full-blown parse on the input to pick out instances of variable use (just look for 'export var', 'unset var', 'var=', and the like). You'd also want to take into account special variables like $RANDOM and $HOSTNAME, but that's pretty trivial.

Thank you, and thanks for the suggestion. Yes, it should be possible to keep the SC2154 check. I probably just disabled it as a quick fix when first started out. I'm tracking this in https://github.com/HenrikBengtsson/shellcheck-repl/issues/15.

> You'd also want to take into account special variables like $RANDOM and $HOSTNAME, but that's pretty trivial.

It seems like ShellCheck is already aware of these special Bash variable, e.g. 'echo $RANDOM' will not trigger SC2154 (or even SC2086 that otherwise asks you to quote variables).

I am not an expert, so forgive me for asking, Does Zsh also has this pitfalls?