Easily Find Bugs In Shell Scripts With ShellCheck

Brief: ShellCheck is a handy utility tool that warns you of possible syntax and simple logical errors, without executing. You can also use it with Atom text editor.

As we all know, the terminal is an inseparable part of the Linux system. And behind everything we do in the terminal, there is the shell — a program that interacts with the operating system itself. Every command we enter in the terminal is ultimately interpreted by the shell and then it acts accordingly.

ShellCheck command line tool

Shell as a Programming Language

There is more to shell though. A UNIX shell is not a command interpreter only, it serves as a programming language too! Shell as a programming language provides variables, flow control constructs, quoting and functions.

A file containing shell commands and codes is called Shell Script. So, you can accomplish various complex tasks and automation for you system using shell scripts.

Anyway, as we are humans, we make mistakes. So obviously, while writing shell scripts you will make mistakes too, such as – syntactical errors, typos, bad patterns etc. Here, we are going to know about the tool — ShellCheck — that will help us with that.

ShellCheck

ShellCheck is a static analysis tool for you shell scripts. It is open-source and developed mainly for Bash or sh scripts. Now you might wonder, what the heck is Static Analysis? Well, static analysis is a method for debugging a program by examining its (static) codes rather than running/executing it.

Let’s take a look at what types of things ShellCheck checks for:

  • Quoting: Shell has specific ways for using variables and strings. If you’ve used them incorrectly ShellCheck will warn you.
  • Conditionals: It will check for incorrect uses of test statements.
  • Frequently Misused Commands: ShellCheck will warn you about incorrectly used shell commands.
  • Common Beginner’s Mistakes: Various syntax errors that beginner Shell users make as Shell uses various strict syntax rules that differ from other programming languages. For example: you can’t put spaces around the = sign while declaring variables. Most languages wouldn’t care but here it matters.
  • Style: ShellCheck will provide suggestions for improving your code’s style.
  • Data & Typing errors: Various issues related to data and typing.
  • Robustness, Portability & Others: ShellCheck will give suggestions for making your scripts robust, warn you about portability issues with different shells (bash,sh,ksh,dash etc.) and various other things.

For documentation and various useful information, do not forget to check the ShellCheck GitHub Project:

ShellCheck on GitHub

Installing ShellCheck

For offline usage, you can install ShellCheck package on your system. On Ubuntu, enter the following command in your terminal:

sudo apt install shellcheck

For other distributions, check the GitHub ReadMe Installing section.

If you use Atom text editor, you can also use ShellCheck within you editor window on-the-fly. Just install the necessary atom packages for ShellCheck:

apm install linter linter-shellcheck

ShellCheck supports other editors, such as – Vim, Emacs, Sublime etc., as well. You can find a complete list on the GitHub ReadMe section.

Using ShellCheck

Online

You can use ShellCheck online without installing anything on your system (you will need a web browser though, haha!).

  • Copy your shell script’s codes.
  • Visit the ShellCheck website.
  • Paste your codes in the editor.
  • It will show the ShellCheck output.

Here’s an example:

Using ShellCheck Online
Using ShellCheck Online

Command-line interface

After installing the necessary packages for ShellCheck on your system, you can use it from your terminal without any net connectivity. Just type the following command:

shellcheck <filename>

Replace the <filename> part with the filename of your shell script.

ShellCheck Command-line interface
ShellCheck Command-line interface

Editor Extensions

After installing necessary extensions for your text editor, you can see the ShellCheck output on-the-fly, without running the shellcheck command manually. Here’s how it looks on the Atom editor:

Atom with Linter-ShellCheck
Atom with Linter-ShellCheck

Pros & Cons

Whether you are a seasoned Shell programmer or just a beginner, you have everything to gain and nothing to lose by using ShellCheck. Whenever you are writing shell scripts, check it with ShellCheck, you will be able to catch errors and bugs early and overall be more productive with Linux.


Have you ever tried to write a shell script before? Did you use ShellCheck? If you did, how was your experience? Do share with us! :)

0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
LVM: Remove a Filesystem / Logical Volume
Viewed 2593 times since Sat, Jun 2, 2018
HowTo: Kill TCP Connections in CLOSE_WAIT State
Viewed 15802 times since Thu, Feb 14, 2019
Extending Linux LVM partitions - scripts
Viewed 6552 times since Sun, May 20, 2018
Using IOzone for Linux disk performance analysis
Viewed 7880 times since Wed, Jul 25, 2018
How to run command or code in parallel in bash shell under Linux or Unix
Viewed 3253 times since Tue, Aug 6, 2019
RHEL: Crash kernel dumps configuration and analysis on RHEL 5
Viewed 7267 times since Sat, Jun 2, 2018
systemd Auto-restart a crashed service in systemd
Viewed 3222 times since Fri, Jan 17, 2020
Linux / UNIX: Run Command a Number of Times In a Row
Viewed 15918 times since Tue, Aug 6, 2019
Linux – How to check the exit status of several piped commands
Viewed 2967 times since Wed, Jul 25, 2018
stunnel Howto A Guide to create SSL access to a HTTP-only webserver with stunnel
Viewed 2724 times since Fri, Sep 28, 2018