{"id":214570,"date":"2021-03-30T16:00:56","date_gmt":"2021-03-30T13:00:56","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-use-shfmt-to-format-shell-scripts-better-cloudsavvy-it\/"},"modified":"2021-03-30T16:00:56","modified_gmt":"2021-03-30T13:00:56","slug":"how-to-use-shfmt-to-format-shell-scripts-better-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-use-shfmt-to-format-shell-scripts-better-cloudsavvy-it\/","title":{"rendered":"#How to Use shfmt to Format Shell Scripts Better \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2d91e6366a9\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2d91e6366a9\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-shfmt-to-format-shell-scripts-better-cloudsavvy-it\/#What_Is_shfmt\" >What Is shfmt?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-shfmt-to-format-shell-scripts-better-cloudsavvy-it\/#Installing_shfmt\" >Installing shfmt<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-shfmt-to-format-shell-scripts-better-cloudsavvy-it\/#Using_shfmt\" >Using shfmt<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-shfmt-to-format-shell-scripts-better-cloudsavvy-it\/#Wrapping_up\" >Wrapping up<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Use shfmt to Format Shell <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">Script<\/a>s Better \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4038\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/07\/add8ac45.png?width=1200&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Bash Shell\" width=\"1400\" height=\"600\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Yes, formatting is necessary and helps tremendously with understanding complex code. However, for those who write shell scripts often, double checking formatting can become a tedious task. This article will show you how to shortcut the work using shfmt!<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"What_Is_shfmt\"><\/span>What Is <i>shfmt<\/i>?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Developed by Dustin Krysak, shfmt is a Shell formatter, parser and interpreter. The project itself is <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/mvdan\/sh\">hosted on GitHub<\/a> and has a clear README and cleanly presented repository. The tool was developed in Go and supports the POSIX, Bash and mksh shells. This makes shfmt a truly universal program instead of being restricted to Bash only.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Installing_shfmt\"><\/span>Installing <i>shfmt<\/i><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To install <i>shfmt<\/i> on your snap-enabled Linux distribution (Like Ubuntu and Mint), execute the following command in your terminal:<\/p>\n<pre>sudo snap install shfmt&#13;\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10150\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/03\/460a52ba.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Installing shfmt with snap\" width=\"532\" height=\"54\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>To install <i>shfmt<\/i> on your RedHat\/Yum based Linux distribution (Like RHEL, Centos and Fedora), execute the following commands in your terminal:<\/p>\n<p>Note: you will have to reboot your machine (or logout and re-login) after executing the first command and before executing the next commands.<\/p>\n<pre>sudo dnf install snapd&#13;\nsudo snap install snap-store&#13;\nsudo snap install shfmt&#13;\n<\/pre>\n<p>On RHEL and Centos you may also have to install the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/snapcraft.io\/install\/snapd\/rhel\">EPEL repository first<\/a>.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Using_shfmt\"><\/span>Using shfmt<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>One the snap package is installed, you can start using shfmt.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10151\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/03\/010c99bb.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"shfmt - a compact tool for Shell formatting\" width=\"694\" height=\"630\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Let us define a very poorly formatted and written script as <code>test.sh<\/code> as follows:<\/p>\n<pre>#!\/bin\/bash__&#13;\n echo 'not well formatted line 1'&#13;\n  echo 'not well formatted line 2'&#13;\necho 'this line has extra spaces on the end &gt; '            &#13;\nfunc() {&#13;\n  echo 'more unneeded spaces'&#13;\n              echo 'way out'&#13;\n }&#13;\nfunc ()&#13;\n<\/pre>\n<p>There are several issues with this script, the most prominent being the formatting thereof. But there is also an error\/bug in the script: The function call <code>func<\/code> on the last line is followed by more brackets. A function call in Bash (rather than a function definition) should only have the name, not the brackets. It is a bit higher that the proper function definition happened.<\/p>\n<p>Let\u2019s see what <code>shfmt<\/code> thinks of this.<\/p>\n<pre>shfmt test.sh&#13;\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10152\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/03\/290ec395.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"A practical example for shfmt with error on incorrect function call\" width=\"443\" height=\"229\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Whereas the output looks a little cryptic, note that the term <code>foo<\/code> (used here) and <code>bar<\/code> (not used here now) are often used in IT circles to indicate\/represent any like idiom or element. <code>foo<\/code> here really refers to <code>func<\/code>.<\/p>\n<p>Even then, the message remains a little cryptic until we realize, looking at the last line, that what is really happening is the start of a function definition (and not a function call) because the two brackets were included. This then explains why the message is telling us that something more is expected; <i>must be followed by a statement<\/i>. <code>shmft<\/code> is looking here for something like <code>func(){ some_command[s]; }<\/code>.<\/p>\n<p>Bingo! This increases the functionality of <code>shfmt<\/code> to being a shell script validation\/checker tool, though likely nowhere near a comprehensive one as the one we wrote about in using shellcheck to find and fix scripting bugs. Still, very handy indeed!<\/p>\n<p>We fix our bug and now the input script <code>test.sh<\/code> reads as follows:<\/p>\n<pre>#!\/bin\/bash__&#13;\n echo 'not well formatted line 1'&#13;\n  echo 'not well formatted line 2'&#13;\necho 'this line has extra spaces on the end &gt; '            &#13;\nfunc() {&#13;\n  echo 'more unneeded spaces'&#13;\n              echo 'way out'&#13;\n }&#13;\nfunc&#13;\n<\/pre>\n<p>We again execute <code>shfmt<\/code> against the code and receive a much more suitable, and well formatted, output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10153\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/03\/e1376426.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using a two space indent with shfmt using the -i option\" width=\"413\" height=\"372\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Great. We can now take this one level further and indicate to <code>shfmt<\/code> that we would like to use an indent\/tab width of two spaces instead of a full tab. I always write code using two spaces as an indent\/tab width, and use an additional space where a command on the next line closely relates to the previous one, like a continued command etc. though this does not happen often. Over more than 10 years, I have found the two spaces to be the personal and shared projects ideal.<\/p>\n<p>Everyone and every project has to find their own ideal syntax, but note that if you use a large tab (8 spaces) like the formatting presented by <code>shfmt<\/code> in the example above, your code may become harder to read easily.<\/p>\n<p>We will set the indent\/tab width to two spaces using the <code>-i<\/code> option (which the <code>--help<\/code> defines as <i>indent: 0 for tabs (default), &gt;0 for number of spaces<\/i>): <code>shfmt -i 2 test.sh<\/code> which will render the script as follows:<\/p>\n<pre>#!\/bin\/bash__&#13;\necho 'not well formatted line 1'&#13;\necho 'not well formatted line 2'&#13;\necho 'this line has extra spaces on the end &gt; '&#13;\nfunc() {&#13;\n  echo 'more unneeded spaces'&#13;\n  echo 'way out'&#13;\n}&#13;\nfunc&#13;\n<\/pre>\n<p>Great! However, we note that <code>shfmt<\/code> did not pickup on our deliberate error: <code>#!\/bin\/bash__<\/code> is not correct, and should read <code>#!\/bin\/bash<\/code> instead.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10154\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/03\/97d82865.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The error both shfmt and shellcheck missed\" width=\"670\" height=\"256\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>There is thus still a use case for using shellcheck to find script bugs besides using <code>shfmt<\/code> to format scripts better. However, interestingly, in this particular case, even <code>shellcheck<\/code> failed to notice the issue. This shortcoming was reported to the shellcheck team so in due time this may be fixed.<\/p>\n<p>If you are interested in learning more about Linux, you can review the Bash Automation &amp; Scripting Basics <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/watch-movies-tv-seriess\/\" data-internallinksmanager029f6b8e52c=\"8\" title=\"Watch Movies &amp; TV Series\" target=\"_blank\" rel=\"noopener\">Series<\/a>, as well as the Bash Loops: for, while, and until Bash Loops: for, while, and until and Exporting Variables in Bash: the Why and How articles.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Wrapping_up\"><\/span>Wrapping up<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Being able to write clean, well-formatted, and bug-free scripts becomes an easier job when you use a shell formatting tool like <code>shfmt<\/code> and a bug\/error checker like <code>shellcheck<\/code>. Even then, as we saw, some things may go unnoticed even up to the moment where you run the script for the first time. <code>shfmt<\/code> is a small but effective utility which will help you format your scripts and code in accordance with your selected indents. <strong>Enjoy!<\/strong>\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/10149\/how-to-use-shfmt-to-format-shell-scripts-better\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Use shfmt to Format Shell Scripts Better \u2013 CloudSavvy IT&#8221; Yes, formatting is necessary and helps tremendously with understanding complex code. However, for those who write shell scripts often, double checking formatting can become a tedious task. This article will show you how to shortcut the work using shfmt! What Is shfmt? Developed&#8230;<\/p>\n","protected":false},"author":1,"featured_media":214571,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/07\/add8ac45.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-214570","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/214570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=214570"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/214570\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/214571"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=214570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=214570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=214570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}