Using `shellcheck` to lint your bash/sh scripts.
Tags: bash, devops, linting, bestpractice
Published: 2018-11-27 18:16:35

---
title: Using `shellcheck` to lint your bash/sh scripts.
published: true
description: Being involved in technology, specifically web, it does not take long we have to write a bash (or shell) script.
tags: #bash, #devops, #linting, #best-practice 
header_image: https://blog.davidjeddy.com/wp-content/uploads/2018/11/linting_Bash_from_tutoriaLinux_on_youtube-930x614.png
---

<!-- wp:heading -->
<h2>Opening</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Being involved in technology, specifically web, it does not take long we have to write a bash (or shell) script. If you work with server side technology this event happens very soon after logging into a server. As a Dev(Sec)Ops practitioner linting code during the a CI/CD process is a basic requirement for me personally. Until recently linting Bash/sh code for me was a painful process. However, like so much else it was just a matter of time before a solution was found to automate the process. Enter '<a href="https://github.com/koalaman/shellcheck">shellcheck</a>'. </p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Installation</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>For this quick demo I am running <a href="http://releases.ubuntu.com/18.04.1/">Ubuntu 18.04</a>. The installation process is very straight forward, APT work for this.</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">sudo apt-get install -y shellcheck<br></pre>
<!-- /wp:preformatted -->

<!-- wp:image {"id":1222} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2018/11/screely-shellcheck_1.png" alt="" class="wp-image-1222"/><figcaption>That was easy.</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Once executed we should see the standard install output. Just to make sure everything completed as expected I execute a quick version check.</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">shellcheck --version</pre>
<!-- /wp:preformatted -->

<!-- wp:image {"id":1225} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2018/11/screely-shellcheck_3.png" alt="" class="wp-image-1225"/><figcaption>Worked as expected.</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>And has hoped <a href="https://github.com/koalaman/shellcheck">shellcheck</a> is indeed installed.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Options</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Getting to the options of <a href="https://github.com/koalaman/shellcheck">shellcheck</a> is as easy as the installation. A quick '--help' argument provides us with the list of execution options.</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">shellcheck --help</pre>
<!-- /wp:preformatted -->

<!-- wp:image {"id":1224} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2018/11/screely-shellcheck_2-1.png" alt="" class="wp-image-1224"/><figcaption>Neat, colors and formatting!</figcaption></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>Usage</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>So far so good. Now lets look at how it executes.  I has a BASH file in my home directory that provides the SVN history of a file so I used that as a demo.</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">shellcheck ./svn_file_history.sh</pre>
<!-- /wp:preformatted -->

<!-- wp:image {"id":1226} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2018/11/screely-shellcheck_4.png" alt="" class="wp-image-1226"/><figcaption>Oh man, I suck at BASH scripting.</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Hey, that's pretty neat. Give it is standard output it would be easy to pipe these messages to a reporting system or quality gate process. Nice.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Closing</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><a href="https://github.com/koalaman/shellcheck">shellcheck</a> is one of those tools that makes life much easier, as long as you know about it before trying to write your own monster of a syntax checker.  Easy to install, easy to use, easy to integrate with it becomes yet another quality and security insurance step along the development pipeline.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Share your favorite linter in the comments so we can all learn.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Additional Resources</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><a href="https://en.wikipedia.org/wiki/Lint_(software)">https://en.wikipedia.org/wiki/Lint_(software)</a></li><li><a href="https://nickjanetakis.com/blog/here-is-why-you-should-quote-your-variables-in-bash">https://nickjanetakis.com/blog/here-is-why-you-should-quote-your-variables-in-bash</a></li><li><a href="https://github.com/koalaman/shellcheck">https://github.com/koalaman/shellcheck</a></li><li>Header Image from <a href="https://www.youtube.com/watch?v=lbMsFXMnqNY">https://www.youtube.com/watch?v=lbMsFXMnqNY</a><a href="https://nickjanetakis.com/blog/here-is-why-you-should-quote-your-variables-in-bash"></a></li></ul>
<!-- /wp:list -->