{"id":8622,"date":"2020-03-09T11:00:35","date_gmt":"2020-03-09T15:00:35","guid":{"rendered":"http:\/\/esr.ibiblio.org\/?p=8622"},"modified":"2020-03-09T14:12:31","modified_gmt":"2020-03-09T18:12:31","slug":"shellcheck-boosting-the-signal","status":"publish","type":"post","link":"http:\/\/esr.ibiblio.org\/?p=8622","title":{"rendered":"shellcheck: boosting the signal"},"content":{"rendered":"<p>I like code-validation tools, because I hate defects in my software and I know that there are lots of kinds of defects that are difficult for an unaided human brain to notice.  <\/p>\n<p>On my projects, I throw every code validater I can find at my code.  Standbys are cppcheck for C code, pylint for Python, and go lint for Go code.  I run these frequently &#8211; usually they&#8217;re either part of the &#8220;make check&#8221; I use to run regression tests, or part of the hook script run when I push changes to the public repository.<\/p>\n<p>A few days ago I found another validator that I now really like: <a href=\"https:\/\/www.shellcheck.net\/\">shellcheck<\/a>  Yes, it&#8217;s a lint\/validator for shell scripts &#8211; and in retrospect shell, as spiky and irregular and suffused with multilevel quoting as it is, has needed something like this for a long time.<\/p>\n<p>I haven&#8217;t done a lot of shell scripting in the last couple of decades. It&#8217;s not a good language for programming at larger orders of magnitude than 10 lines or so &#8211; too many tool dependencies, too difficult to track what&#8217;s going on. These problems are why Perl and later scripting language became important; if shell had scaled up better the space they occupy would have remained shell code as far as the eye can see.<\/p>\n<p>But sometimes you write a small script, and then it starts to grow, and you can end up in an awkward size range where it isn&#8217;t <em>quite<\/em> unmanageable enough to drive you to port it to (say) Python yet. I have some cases like this in the reposurgeon suite.<\/p>\n<p>For this sort of thing a shell validater\/linter can be a real boon, enabling you to have much more confidence that you&#8217;ll catch silly errors when you modify the script, and actually increasing the upper limit of source-line count at which shell remains a viable programming language.<\/p>\n<p>So it is an excellent thing that shellcheck is a solid and carefully-thought-out piece of work.  It does catch  lot of nits and potential errors, hardening your script against cases you probably haven&#8217;t tested yet. For example. it&#8217;s especially good at flagging constructs that will break if a shell variable like $1 gets set to a value with embedded whitspace.<\/p>\n<p>It has other features you want in a code validator, too. You can do line-by-line suppression of specific spellcheck warnings with magic comments, telling the tool &#8220;Yes, I really meant to do that&#8221; so it will shut up.  This means when you get new warnings they are really obvious.<\/p>\n<p>Also, it&#8217;s fast.  Fast enough that you can run it on all your shellscripts up front of all your regular regression tests and probably barely ever notice the time cost.<\/p>\n<p>It&#8217;s standard practice for me to have a &#8220;make check&#8221; that runs code validators and then the regression tests. I&#8217;m going back and adding shellcheck validation to those check productions on all my projects that ship shell scripts.  I recommend this as a good habit to everybody.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I like code-validation tools, because I hate defects in my software and I know that there are lots of kinds of defects that are difficult for an unaided human brain to notice. On my projects, I throw every code validater I can find at my code. Standbys are cppcheck for C code, pylint for Python,&hellip; <a class=\"more-link\" href=\"http:\/\/esr.ibiblio.org\/?p=8622\">Continue reading <span class=\"screen-reader-text\">shellcheck: boosting the signal<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-8622","post","type-post","status-publish","format-standard","hentry","category-software","entry"],"_links":{"self":[{"href":"http:\/\/esr.ibiblio.org\/index.php?rest_route=\/wp\/v2\/posts\/8622","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/esr.ibiblio.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/esr.ibiblio.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/esr.ibiblio.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/esr.ibiblio.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8622"}],"version-history":[{"count":4,"href":"http:\/\/esr.ibiblio.org\/index.php?rest_route=\/wp\/v2\/posts\/8622\/revisions"}],"predecessor-version":[{"id":8626,"href":"http:\/\/esr.ibiblio.org\/index.php?rest_route=\/wp\/v2\/posts\/8622\/revisions\/8626"}],"wp:attachment":[{"href":"http:\/\/esr.ibiblio.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/esr.ibiblio.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8622"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/esr.ibiblio.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}