1. Home
  2. Linux

How to debug Linux Bash scripts with ShellCheck

Are you having trouble with a Bash script you wrote for your Linux PC? Instead of looking through countless threads on StackExchange for programming help, consider using the ShellCheck tool to debug Linux Bash scrips and automatically find mistakes in your code.

Get ShellCheck

ShellCheck is a useful tool, but it doesn’t come with your Linux operating system out of the box. Instead, if you’d like to use it, you’re going to have to install it first. In this tutorial, we’ll cover how to install ShellCheck from various official software sources. Also, we’ll also show you how to get the program working via the generic Linux binary.

Open up a terminal window and update your packages. Then, follow the instructions that correspond to your Linux operating system.

Ubuntu

ShellCheck is in the official Ubuntu software sources, so installing it is quite easy. In a terminal, use the Apt package manager and get the latest version of ShellCheck working.

sudo apt install shellcheck

Debian

Debian Stable has an older version of ShellCheck ready for users to install. Keep in mind that this version is held back due to how Debian does things.

Note: want a newer version of the ShellCheck program? Follow our guide and learn about how to get more modern software on Debian.

sudo apt-get install shellcheck

Arch Linux

To get ShellCheck on Arch Linux, you’ll first need to enable the “Community” software repository. Enabling “Community” requires editing the Pacman.conf file. To edit the file, open up Pacman.conf in Nano.

sudo nano /etc/pacman.conf

Scroll through (with the arrow keys) and find the “Community” section of the file. Remove all instances of the # symbol (about three lines). Then, save the edits by pressing the Ctrl + O button.

With the new edits to Pacman.conf taken care of, press Ctrl + X to close Nano. Then, resync Pacman to set up access to the Arch Linux “Community” repository.

sudo pacman -Syyuu

Finally, install ShellCheck to Arch Linux with:

sudo pacman -S shellcheck

Fedora

To get the ShellCheck script analysis tool on Fedora Linux, open up a terminal and use the DNF package management tool.

sudo dnf install ShellCheck

OpenSUSE

Installing ShellCheck on OpenSUSE requires no special steps, as it is in the distribution’s main software sources. To get it, enter the following Zypper package command in a terminal window.

sudo zypper install ShellCheck

Generic Linux

Users of lesser-known Linux distributions that want access to the ShellCheck script analysis tool must install the universal binary release. Luckily, this binary release is easy to install and requires no tinkering with source code. To get it working, follow the steps below!

Step 1:  export the version of ShellCheck you’re about to download to your terminal environment.

export scversion="stable" # or "v0.4.7", or "latest"

Step 2: using the wget download tool, grab the latest ShellCheck binary release.

wget "https://storage.googleapis.com/shellcheck/shellcheck-${scversion}.linux.x86_64.tar.xz"

Step 3: extract the ShellCheck TarXZ archive with the tar command.

tar --xz -xvf shellcheck-"${scversion}".linux.x86_64.tar.xz

Step 4: install ShellCheck to your Linux PC with the cp command.

cp shellcheck-"${scversion}"/shellcheck /usr/bin/

Debug scripts with ShellCheck

ShellCheck’s primary purpose is to look over a script (usually Bash), detect misused commands and offer up corrections. For basic usage, do the following.

First, CD to where the script you’d like to check is on your Linux PC. In this tutorial, our script is in ~/shell-scripts. Yours may differ.

cd ~/shell-scripts

Run the ls command and view all of the files in the current directory.

Note: if you intend to use ShellCheck in a directory with multiple file types, it’s a good idea to combine grep and ls together. Using these two commands together allows you to filter out all non-script files.

ls

Or, combine with grep to view only script files.

ls | grep ".sh"
ls | grep ".bash"

Find the name of a script file in the ls prompt and highlight the file name with the mouse. When ready, run it through ShellCheck and check your code.

shellcheck name-of-file.sh

or

shellcheck name-of-file.bash

Immediately after the shellcheck command runs, it will detect issues with your code and put it in the terminal prompt. Read through and learn how to improve your programming by following the suggestions it gives you.

Save ShellCheck report to a file

Getting an instant report in the terminal for your bash programming is super useful however, you may lose it if you close the terminal. To get around losing your shellcheck report, we recommend saving the output to a text file, by running the program in combination with the >> feature in the terminal.

shellcheck name-of-file.sh >> ~/Documents/shellcheck-report-name-of-file.sh

or

shellcheck name-of-file.bash >> ~/Documents/shellcheck-report-name-of-file.bash

View your saved report in the terminal via the cat command.

cat ~/Documents/shellcheck-report-name-of-file.sh | more

or

cat ~/Documents/shellcheck-report-name-of-file.bash | more

Leave a comment