Go to file
2018-05-06 14:33:39 +02:00
test Support arithmentic expansion 2018-03-31 16:23:07 +02:00
ex-artificial.png Readme: Add screenshots and a few more words 2017-07-31 00:33:55 +02:00
ex-realworld.png Readme: Add screenshots and a few more words 2017-07-31 00:33:55 +02:00
how_to_do_things_safely_in_bash.md how to do things safely: More on errexit 2018-05-06 14:33:39 +02:00
naziquote.rs Support arithmentic expansion 2018-03-31 16:23:07 +02:00
README.md Readme: Add screenshots and a few more words 2017-07-31 00:33:55 +02:00

Naziquote

A bash syntax highlighter that encourages (and can fix) proper quoting of variales.

Because rewriting scripts to adhere to the always use quotes principle can be daunting:

real-world example

Above: Selected portions of xdg-desktop-menu as highlighted by Naziquote. The foreground colors are syntax highlighting, whereas the background colors (green and red) show characters that Naziquote would have added or removed if let loose with the --besserwisser option. Below: An artificial example that shows more tricky cases and special features.

artificial example

Why

A variable in bash is like a hand grenade take off its quotes, and it starts ticking. Hence, rule zero of bash pitfalls: Always use quotes.

Name

Temptation; apologies for that. "Nazi" alludes to strict adherence to principles, with little room for excuses. By that notion, you can tell that "naziquote", no matter who put this compound verb together, must mean to quote systematically without much mercy or afterthought.

That's Naziquote in a nutshell: Naziquote will break code that relies on unquotedness, and Naziquote is NOT sorry for that the premise for using Naziquote is to apply afterthought manually.

Regrettably, "naziquote" may not be palatable everywhere, but I couldn't call it "bash cleaner" either, as that means "poo smearer" in Norwegian.

Prior art

  • Shellcheck is a wonderful tool to detect, and give general advice, about brittle bash code. The only thing missing is something to say yes with, and apply those advice (assuming proper review of course).

  • I asked this SO question, for a tool that could rewrite bash scripts with proper quoting. One answerer beat me to it. But if it was me, I would do a syntax highlighter in the same tool (as a way to see if the parser gets lost, and make the most out of the parser, because bash is like quantum mechanics nobody really knows how it works).

Build

rustc naziquote.rs

Usage advice

Don't apply --besserwisser blindly; code review is still necessary: A script that relies on unquoted behavior (implicit word splitting and glob expansion from variables and command substitutions) to work as intended will do none of that after getting the --besserwisser treatment!

In that unlucky case, ask yourself whether the script has any business in doing that. All too often, it's just a product of classical shellscripting, and would be better off rewritten, such as by using vectors. Even in the opposite case, say the business logic involves word splitting; that can still be done without invoking globbing. In short: There is always a better way than the forbidden syntax (if not more explicit), but some times, a human must step in to rewrite. See how, in the accompanying how to do things safely in bash.