{"id":793,"date":"2019-07-29T03:45:32","date_gmt":"2019-07-29T03:45:32","guid":{"rendered":"http:\/\/www.vidarholen.net\/contents\/blog\/?p=793"},"modified":"2019-07-29T03:45:32","modified_gmt":"2019-07-29T03:45:32","slug":"whats-new-in-shellcheck-v0-7-0","status":"publish","type":"post","link":"https:\/\/www.vidarholen.net\/contents\/blog\/?p=793","title":{"rendered":"What&#8217;s new in ShellCheck v0.7.0?"},"content":{"rendered":"<p>ShellCheck v0.7.0 has just been released. In addition to the usual \u201cbug fixes and improvements\u201d, there is a set of new features:<\/p>\n<h3 id=\"autofixes\">Autofixes<\/h3>\n<p>A few select warnings now come with auto-fixes. In the most straight-forward case, ShellCheck shows you what it thinks the line ought to be:<\/p>\n<pre><code>In foo line 2:\necho \"File size: $(stat -c %s $1)\"\n                              ^-- SC2086: Double quote to prevent globbing and word splitting.\n\nDid you mean:\necho \"File size: $(stat -c %s \"$1\")\"<\/code><\/pre>\n<p>To actually apply the fixes, you can use ShellCheck\u2019s new <code>diff<\/code> output format, which outputs standard Unified Diffs that can be piped into tools like <code>git apply<\/code> and <code>patch<\/code>:<\/p>\n<pre><code>$ shellcheck -f diff foo\n--- a\/foo\n+++ b\/foo\n@@ -1,2 +1,2 @@\n #!\/bin\/sh\n-echo \"File size: $(stat -c %s $1)\"\n+echo \"File size: $(stat -c %s \"$1\")\"<\/code><\/pre>\n<p>For example, to apply only SC2086 fixes to all <code>.sh<\/code> file in a project:<\/p>\n<pre><code>$ shellcheck --include=SC2086 -f diff **\/*.sh | git apply<\/code><\/pre>\n<h3 id=\"optional-checks\">Optional Checks<\/h3>\n<p>ShellCheck now includes a small handful of checks that are off by default. These are intended for subjective issues that a project may choose to enforce:<\/p>\n<pre><code>$ cat foo\n#!\/bin\/sh\n# shellcheck enable=require-variable-braces\nname=World\necho \"Hello $name\"\n\n$ shellcheck foo\nIn foo line 4:\necho \"Hello $name\"\n            ^---^ SC2250: Prefer putting braces around variable references even when not strictly required.\n\nDid you mean:\necho \"Hello ${name}\"<\/code><\/pre>\n<p>For a list of such checks, run <code>shellcheck --list-optional<\/code><\/p>\n<h3 id=\"source-paths\"><code>source<\/code> paths<\/h3>\n<p>ShellCheck now allows you to specify a list of search locations for <code>source<\/code>d scripts using a <code># shellcheck source-path=\/my\/dir<\/code> directive or <code>--source-path<\/code> flag.<\/p>\n<p>This is useful in several cases:<\/p>\n<ul>\n<li>If all the projects\u2019 sourced files are relative to the same directory, you can now specify this directory once instead of having to add <code>source<\/code> directives everywhere.<\/li>\n<li>The special name <code>SCRIPTDIR<\/code> can be specified in a path to refer to the location of the script being checked, allowing ShellCheck to more conveniently discover included files from the same directory. This also works for any path relative to the script\u2019s directory, such as <code>SCRIPTDIR\/..\/include\/<\/code><\/li>\n<li>Absolute paths are also grounded in the source path, so by specifying <code>source-path=\/mnt\/chroot<\/code>, shellcheck will look for <code>. \/bin\/funcs.sh<\/code> in <code>\/mnt\/chroot\/bin\/funcs.sh<\/code>. This is useful when targeting a specific system, such as an embedded one.<\/li>\n<\/ul>\n<h3 id=\"rc-files\">RC files<\/h3>\n<p>Rather than adding directives in each file, you can now set most of the options above in a <code>.shellcheckrc<\/code> file in the project\u2019s root directory (or your home directory). This allows you to easily apply the same options to all scripts on a per-project\/directory basis.<\/p>\n<h3 id=\"bats-and-shflags-support\">Bats and shflags support<\/h3>\n<p>ShellCheck no longer needs any preprocessing to check <a href=\"https:\/\/github.com\/sstephenson\/bats\">Bats<\/a> scripts:<\/p>\n<pre><code>$ cat test.bats\n#!\/usr\/bin\/env bats\n\n@test \"addition using bc\" {\n  result=\"$(echo 2+2 | bc)\"\n  [ \"$result\" -eq 4 ]\n}\n\n$ shellcheck test.bats &amp;&amp; echo \"Success\"\nSuccess<\/code><\/pre>\n<p>A <code>bats<\/code> shebang will be interpreted as \u201cbash\u201d, and <code>@test<\/code> statements will be correctly parsed.<\/p>\n<p>ShellCheck now also recognizes <code>DEFINE_*<\/code> statements from the <code>shflags<\/code> library:<\/p>\n<pre><code>DEFINE_string 'name' 'world' 'name to say hello to' 'n'\n              ^----^ SC2034: FLAGS_name appears unused. Verify use (or export if used externally).<\/code><\/pre>\n<hr>\n<p>For a more extensive list of changes, check out the <a href=\"https:\/\/github.com\/koalaman\/shellcheck\/blob\/b7b4d5d29e401858074b0d36d7bb53da58c3932d\/CHANGELOG.md\">ChangeLog<\/a>.<\/p>\n<p>Happy ShellChecking!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ShellCheck v0.7.0 has just been released. In addition to the usual \u201cbug fixes and improvements\u201d, there is a set of new features: Autofixes A few select warnings now come with auto-fixes. In the most straight-forward case, ShellCheck shows you what it thinks the line ought to be: In foo line 2: echo &#8220;File size: $(stat &hellip; <a href=\"https:\/\/www.vidarholen.net\/contents\/blog\/?p=793\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;What&#8217;s new in ShellCheck v0.7.0?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[6,4],"tags":[46],"class_list":["post-793","post","type-post","status-publish","format-standard","hentry","category-basic-linux","category-linux","tag-shellcheck"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/posts\/793","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=793"}],"version-history":[{"count":8,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/posts\/793\/revisions"}],"predecessor-version":[{"id":801,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/posts\/793\/revisions\/801"}],"wp:attachment":[{"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}