Using shellcheck to find and fix scripting errors

Contents

Shellcheck is a great Linux shell script analysis tool that can be used to detect common programming errors. Shellcheck analyzes scripts and reports errors and warnings, just like what a compiler would do.

What is it Shellcheck?

If you have been a Linux Bash developer for a while, you've probably encountered a fair amount of bugs in your own scripts or in the scripts of others. Introducing bugs into code is likely to happen when humans are developing code. Even the best developers can miss an unforeseen complexity or warning in their code from time to time..

In bash, there is no real compiler like, as an example, and C ++. Despite this, there is a set of tools that can be of great help when developing bash scripts. Once such a tool is shellcheck. This excellent utility will analyze a bash script file and make recommendations based on what is found during its analysis.. It's a bit like having a compiler for bash.

Tools like shellcheck differ in their operation from other runtime tools, as an example run a script with bash -x to see each and every command in the script that is being executed, and that in real time. The reason is that shellcheck will parse the script (file) without actually executing it, de nuevo de manera equivalente a lo que haría un compilador.

For more information on bash -x, es factible que desee leer Bash Automation and Scripting Basics (Part 3), que forma parte de la serie de 3 partes Bash Automation and Scripting Basics.

installing Shellcheck

Install Shellcheck on your Debian-based Linux distribution / Apt (like Ubuntu and Mint), run the following command in your terminal:

sudo apt install shellcheck

Install Shellcheck on your RedHat-based Linux distribution / Yum (like RHEL, Centos and Fedora), run the following command in your terminal:

sudo yum install shellcheck

In a hurry Shellcheck

Una vez que hayamos instalado shellcheck, podemos hacer una prueba simple con un script roto. Primero definimos nuestro guión test.sh as follows:

#! / bin / wash echo 'Bash no es wash echo Más errores para mí "and [ -d ./directory }; than
  echo 'sure! < start
fif

How many bugs can you find? (Tip: there are 8!).

Let’s next see what shellcheck makes of this code:

shellcheck test.sh

Shellcheck output 1

Immediately on the first line it finds an issue with the shebang specification. If you haven’t heard of shebang yet, please checkout our Bash Automation and Scripting Basics Part 1 article. Our pun shebang line #!/bin/wash should be #!/bin/bash. Let’s fix this. Issue 1/8 fixed!

We will also at the same time fix the other two issues immediately recognized by shellcheck: Did you forget to close this single quoted string? for the second line: spot on! Issue 2/8 fixed. For the third issue there is a little confusion as to our/the developers intent for shellcheck, and this is to be expected, as the ' on line 2 opens a string which is only terminated on line 5 when another ' is seen!

As this third issue is thus a result of the second issue, this run will allow us to fix two issues for the time being. Our script now looks like this:

#!/bin/bash
echo 'Bash is not wash'
echo More errors for me"
if [ -d ./directory }; than
  echo 'sure! < start
fif

Let’s run shellcheck on this again after making the corrections and see what the output is.

Shellcheck output 2

In this instance, shellcheck sees that a " is opened on line 3 (even though it is at the end of the line, it is actually an opening double quote as such), and that even at script end (note the line 8 indication, which does not exist in our 6-line script with a single empty line after the last line. Let’s clean up this empty line, and fix the double quoting issue at the start of line 3, which can now be readily understood. Issue 3/8 fixed!

Our script now looks like this:

#!/bin/bash
echo 'Bash is not wash'
echo "More errors for me"
if [ -d ./directory }; than
  echo 'sure! < start
fif

Re-running shellcheck (note how similar again these steps are to using a compiler in other coding languages):

Bash shellcheck output 3

Could not be clearer; The mentioned syntax error was in this if expression and Expected test to end here. We shall do as suggested and change the } to ], making the line read if [ -d ./directory ]; than. Problem 4/8 solved! We ran shellcheck again and now we are presented with the following:

shellcheck salida 4

Another single quote problem. We already know how to solve them. Let's change echo 'sure! < start in order to echo 'sure!' < start (Problem 5/8 solved!) and run shellcheck one more time:

Departure 5 de Shellcheck

Interesante al principio, we see that shellcheck cannot parse a line. Although this may seem like a deficiency in shellcheck, reading a little more, we see that somewhere a then You're lost. Dog! We have put than instead of then. What a careless 😉 mistake It was easily fixed (problem 6/8 solved!). Our script now looks like this:

#!/bin/bash
echo 'Bash is not wash'
echo "More errors for me"
if [ -d ./directory ]; then
  echo 'sure!' < start
fif

And another shellcheck run provides us with other useful information:

Departure 6 de Shellcheck

We have a fault fi! Aha yes fif He won't. Change fif in order to fi in the last line of the script (trouble 7/8) fixed and ran shellcheck once again.

Departure 7 de Shellcheck

A redirection hurdle. Honestly I didn't expect shellcheck to also grasp this error, since < it can also be used in Bash, but sure he did. Actually, our redirect was meant to be > instead of <. Problem 8/8 – all problems – Solved! This brings us to the final script.

#!/bin/bash
echo 'Bash is not wash'
echo "More errors for me"
if [ -d ./directory ]; then
  echo 'sure!' > start
fi

Let's see what Shellcheck thinks now.

Shellcheck output 8

Perfect! And the script works great, from the first execution.

If you review the output of multiple shellcheck commands, you will also notice another very useful feature of shellcheck, especially for beginners: a set of hyperlinks is displayed (links to websites), which can be clicked with the mouse from the terminal window, or you can select (if required)> right click to copy and later paste in a browser. By clicking on said link, will access the proyecto shellcheck GitHub.

I escaped?

If you want to quickly check only the most important alternatives, we suggest you take a look at the --severity={SEVERITY} option, where would you replace {SEVERITY} with one of error, warning, info, style.

Because, if you are just looking for errors and warnings, would use --severity=warning (including upper levels, in this circumstance only error) as an option for shellcheck.

Ending

If there are no problems with the logic in a script, run shellcheck before running the script and fixing all detected problems, a near perfect execution will be ensured on the first attempt. You can even use shellcheck on that coding challenge for your next live bash coding interview!! In this post we explore various problems that might arise in scripts and how shellcheck handles them.

Enjoy error-free scripts!

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.