TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: Let's build Checkstyle for Bash?

59 点作者 a-t-0大约 3 年前
After working with Bash and Shellcheck for a few months, I noticed I could improve my code quality by making it compliant with the Shell Style Guide by Google [0]. While working on that, I thought some aspects of this Shell style guide can be verified automatically, granted some assumptions&#x2F;opinions are formed. So I looked around for linting tools and autoformatters for Bash:<p>Shellcheck: <a href="https:&#x2F;&#x2F;github.com&#x2F;koalaman&#x2F;shellcheck" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;koalaman&#x2F;shellcheck</a><p>From Asynchronous Lint Engine (ALE): <a href="https:&#x2F;&#x2F;github.com&#x2F;dense-analysis&#x2F;ale&#x2F;blob&#x2F;master&#x2F;supported-tools.md" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;dense-analysis&#x2F;ale&#x2F;blob&#x2F;master&#x2F;supported-...</a><p>- bashate: <a href="https:&#x2F;&#x2F;github.com&#x2F;openstack&#x2F;bashate" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;openstack&#x2F;bashate</a><p>- cspell: <a href="https:&#x2F;&#x2F;github.com&#x2F;streetsidesoftware&#x2F;cspell&#x2F;tree&#x2F;main&#x2F;packages&#x2F;cspell" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;streetsidesoftware&#x2F;cspell&#x2F;tree&#x2F;main&#x2F;packa...</a><p>- Bash Language Server: <a href="https:&#x2F;&#x2F;github.com&#x2F;bash-lsp&#x2F;bash-language-server" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;bash-lsp&#x2F;bash-language-server</a><p>- shell -n flag: <a href="https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;bash&#x2F;manual&#x2F;bash.html#index-set" rel="nofollow">https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;bash&#x2F;manual&#x2F;bash.html#index-set</a><p>- sh(shfmt): <a href="https:&#x2F;&#x2F;github.com&#x2F;mvdan&#x2F;sh" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mvdan&#x2F;sh</a><p>- shdoc: <a href="https:&#x2F;&#x2F;github.com&#x2F;reconquest&#x2F;shdoc" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;reconquest&#x2F;shdoc</a><p>From this stack post [1]:<p>- checkbashisms: <a href="http:&#x2F;&#x2F;man.he.net&#x2F;man1&#x2F;checkbashisms" rel="nofollow">http:&#x2F;&#x2F;man.he.net&#x2F;man1&#x2F;checkbashisms</a><p>- shlint: <a href="https:&#x2F;&#x2F;github.com&#x2F;duggan&#x2F;shlint" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;duggan&#x2F;shlint</a> (archived)<p>Prettier: <a href="https:&#x2F;&#x2F;marketplace.visualstudio.com&#x2F;items?itemName=esbenp.prettier-vscode" rel="nofollow">https:&#x2F;&#x2F;marketplace.visualstudio.com&#x2F;items?itemName=esbenp.p...</a><p>Within all these linters and auto-formatters I did not find checks that enforce, for example, the Function Comments of the Shell Style Guide by Google:<p>All function comments should describe the intended API behaviour using:<p><pre><code> Description of the function. Globals: List of global variables used and modified. Arguments: Arguments taken. Outputs: Output to STDOUT or STDERR. Returns: Returned values other than the default exit status of the last command run. </code></pre> Hence, I thought we could make a Bash linting tool that verifies compliance with the Shell Style Guide by Google. To do so, a brief start was made here [2]. It identifies&#x2F;lists elements in that style guide that may be verified automatically. Since Bash has been around longer than me, I think there may be some people better suited for the development of this enhanced linter. Hence, I thought it might be wise, for impact and usability, to share this idea here.<p>What do you say, HN?<p>[0]: <a href="https:&#x2F;&#x2F;google.github.io&#x2F;styleguide&#x2F;shellguide.html" rel="nofollow">https:&#x2F;&#x2F;google.github.io&#x2F;styleguide&#x2F;shellguide.html</a><p>[1]: <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;3668665&#x2F;is-there-a-static-analysis-tool-like-lint-or-perlcritic-for-shell-scripts" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;3668665&#x2F;is-there-a-stati...</a><p>[2]: <a href="https:&#x2F;&#x2F;github.com&#x2F;TruCol&#x2F;checkstyle-for-bash" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;TruCol&#x2F;checkstyle-for-bash</a>

9 条评论

derefr大约 3 年前
I&#x27;ve always been confused that in ${current_year} people are still directly writing huge + complex shell scripts.<p>Compare&#x2F;contrast Javascript, another language we&#x27;re &quot;stuck with&quot;: while you <i>can</i> still directly write it, many people don&#x27;t, instead using transpilers to <i>target</i> Javascript as an &quot;object code&quot; from some other, stricter language (e.g. TypeScript, ClojureScript, arbitrary native languages via Emscripten &#x2F; WASM compiler targets, etc.) Via these languages&#x2F;compilers, everybody who wants compile-time strictness can have it. And you can even get <i>runtime</i> strictness that Javascript itself doesn&#x27;t have, as the checked semantics of these languages will often be &quot;lowered&quot; into the resulting Javascript by generating explicit assertions (usually this is on by default, unless you ask for an optimized build.) But the output is still just &quot;Javascript&quot;, that any browser can run.<p>Why hasn&#x27;t a similar thing happened for POSIX-standard Bourne shell? Why are we seemingly &quot;satisfied&quot; with writing + maintaining 5000-line install.sh scripts in our codebases, with a bunch of extra mental overhead &#x2F; tooling required to keep the code sane and clean; when we could just be writing in a sane and clean language to begin with?<p>Is it just that nobody&#x27;s bothered to create such a language&#x2F;compiler? Do I need to be the one to step up, here?
评论 #30408167 未加载
评论 #30407679 未加载
评论 #30408414 未加载
评论 #30416248 未加载
评论 #30410221 未加载
评论 #30416136 未加载
nickjj大约 3 年前
2 things I&#x27;d love to see in a checker would be:<p>Warn me when I&#x27;m using -h instead of --help. When writing persisted scripts it&#x27;s often more descriptive and self documenting to use long form flags[0] when a tool gives you both options.<p>Warn me when I mix using &quot;-p 8000:8000&quot; and &quot;-p=8000:8000&quot;. I know it&#x27;s technically not possible to give a definitive answer here because supporting spaces vs equals comes down to the CLI tool itself but you can pick up when you mix the 2 styles and leave it up to the script author to pick a style and stick with it when possible.<p>I do a lot of shell scripting in my day to day (ops, etc.) and it&#x27;s always a manual process to give feedback on code reviews when others contribute patches or introduce new scripts.<p>[0]: <a href="https:&#x2F;&#x2F;nickjanetakis.com&#x2F;blog&#x2F;when-to-use-long-word-or-short-letter-command-line-flags" rel="nofollow">https:&#x2F;&#x2F;nickjanetakis.com&#x2F;blog&#x2F;when-to-use-long-word-or-shor...</a>
评论 #30405894 未加载
bashhacker大约 3 年前
Take a look at shfmt and shdoc. One is an auto formatter, the other does autodocs. I wouldn&#x27;t invent yet another docstring format. It&#x27;s already been done. Just check compliance with an existing format.
评论 #30405561 未加载
评论 #30405712 未加载
Etheryte大约 3 年前
Out of pure curiosity, in what context do you write sufficient amounts of Bash scripts that style checking is a worry that needs your attention? While I also write small one-offs or bootstrap scripts here and there, in most cases it&#x27;s my experience that developers opt for other languages for anything beyond small snippets.
评论 #30405664 未加载
评论 #30405463 未加载
评论 #30408112 未加载
评论 #30405403 未加载
ancientsofmumu大约 3 年前
Google&#x27;s styleguide has incorrect bash being used, they are using string comparison operators for numeric comparisons (see the various `$? != 0` and $PIPESTATUS).<p><pre><code> [[ -1 -eq 0-1 ]] &amp;&amp; echo true || echo false [[ -1 == 0-1 ]] &amp;&amp; echo true || echo false </code></pre> <a href="https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;bash&#x2F;manual&#x2F;html_node&#x2F;Bash-Conditional-Expressions.html" rel="nofollow">https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;bash&#x2F;manual&#x2F;html_node&#x2F;Bash-Cond...</a>
评论 #30406067 未加载
hyperupcall大约 3 年前
This is a neat idea, but I&#x27;m not sure I see the need for this specific tool. Shellcheck covers essentially all the errors that are mentioned in the Google Style Guide so I&#x27;d just be using that instead.<p>And as the Guide itself, it is mostly OK, but there are a few things I really don&#x27;t like. For example:<p>- Whatever doc format they use is not as nice to use as shdoc. Namely, you don&#x27;t have to add the spaces to your comment to make it formatted properly<p>- The &#x27;no tabs&#x27; thing is pretty bullshit - Bash is literally the perfect language to use tabs and their &quot;Whatever you do, don’t use tabs&quot; comment really kills me<p>- I don&#x27;t like their recommendation of using `[[`. Only using `[[` for stuff like regex and glob matching makes it much easier to paste code between Bash and POSIX sh.<p>There are also a few things they forgot to mention, like the lastpipe` shopt option when talking about piping while loops. And maybe it would be nice to recommend using local like `local var=`. But I guess this guide is meant more for new people that don&#x27;t know all the pitfalls and stuff<p>But I think I agree there is some need for a more extensible linter. I&#x27;ve been eyeing the Bash LSP, which is based off the Bash Tree Sitter parser, but that has quite a few bugs with parsing that I&#x27;ve been meaning to send PR&#x27;s for.<p>I wonder if `shfmt` has gotten better since I used it. A few years ago, it was quite buggy with prepending extra whitespace whenever I formatted the file
hutrdvnj大约 3 年前
If your problem can be solved neatly with some combination of existing shell utilities, and you just need a bit of glue to join them together, bash is great.<p>The huge weakness of bash is the data mangling that is more complicated than a relatively simple per-row transformation. Splitting, joining, sorting, min&#x2F;max of data structures like arrays, lists and trees are trivial in python, not so in bash. If it&#x27;s not trivially seddable, and awk doesn&#x27;t help either because you have to rearrange tree like structures, you are about to experience a massive pain, because it&#x27;s out of scope of even expertly used associative arrays and you will find yourself reinventing the wheel hard using pretty basic tools.<p>TLDR; As soon as you need to use something more complex than a for loop, switch to Python.
latchkey大约 3 年前
Intellij has a builtin plugin that integrates with ExplainShell, Shfmt, ShellCheck.
eternityforest大约 3 年前
I think we should really be working to deprecate large bash scripts. If you write enough to care about code quality you might want to just use Python.<p>I&#x27;d say look at things like Oil(Is that project ever going to be the next big thing like it claims?).... but writing large scripts in ANY shell doesn&#x27;t seem like the best plan.<p>What about an Ansible linter? Do those exist? I&#x27;m starting to suspect Ansible might be a better choice for a lot of what people do with bash, even if you are running om a desktop.
评论 #30405562 未加载
评论 #30405672 未加载
评论 #30405473 未加载
评论 #30405514 未加载
评论 #30406166 未加载