How to install ShellCheck on FreeBSD – A shell script static analysis tool

ShellCheck is easy to use, free, and open-source static analysis tool that automatically finds bugs in your shell scripts. If you write shell scripts for automation or containers, you need this tool. Let us see how to install and use ShellCheck on the FreeBSD development Unix server.

ShellCheck released under GPLv3. We can install it using a package manager. It also works as an integrated linter in notable code editors. ShellCheck tool is written in Haskell programming language. Since I am using FreeBSD, I am going to install and use ShellCheck on FreeBSD operating systems. Open the terminal app and then type the following commands.
Tutorial details
Difficulty Easy (rss)
Root privileges Yes
Requirements FreeBSD {Linux user see this page}
Time 5m

Installing ShellCheck on FreeBSD system

First, update all packages on FreeBSD by applying security updates using pkg/freebsd-update:
sudo pkg update
sudo pkg upgrade

Search for ShellCheck

Type the following pkg command:
sudo pkg search shellcheck
Let us find information about the package on your FreeBSD box using the cat command:
cat /usr/ports/devel/hs-ShellCheck/pkg-descr

The goals of ShellCheck are:
 * To point out and clarify typical beginner's syntax issues, that causes
   a shell to give cryptic error messages.
 * To point out and clarify typical intermediate level semantic problems,
   that causes a shell to behave strangely and counter-intuitively.
 * To point out subtle caveats, corner cases and pitfalls, that may cause
   an advanced user's otherwise working script to fail under future


All you have to do is:
sudo pkg install hs-ShellCheck
## OR use FreeBSD ports ##
cd /usr/ports/devel/hs-ShellCheck/
make install clean

Linux specific installation notes are here.


Naturally, you need a shell script to test and use ShellCheck. Please note that this linting tool was created for sh/bash scripts only. It’s mainly focused on handling typical beginner and intermediate level syntax errors. It will provide suggestions for common pitfalls where the shell gives a cryptic error message. We can fix strange behavior in our code, but it also reports on a few more advanced issues where corner cases can cause delayed failures.

How do I use ShellCheck on FreeBSD or Unix-like system?

Consider the following shells script:

. /home/scripts/.keychain/aws-ec2-server42-keys-sh
[ "$1" != "" ] && /usr/local/bin/rsnapshot "$1" || { echo "Usage: $0 [ahourly|bdaily|cmontly|dweekly|eyearly]"; exit 999; }

Run it as follows:
shellcheck sshlogin
shellcheck -x sshlogin

Here is what we see:

In sshlogin line 1:
 ^-- SC1113: Use #!, not just #, for the shebang.
In sshlogin line 2:
. /home/scripts/.keychain/aws-ec2-server42-keys-sh
  ^-- SC1091: Not following: /root/.keychain/nixcraft-m6700-sh was not specified as input (see shellcheck -x).
In sshlogin line 3:
[ "$1" != "" ] && /usr/local/bin/rsnapshot "$1" || { echo "Usage: $0 [ahourly|bdaily|cmontly|dweekly|eyearly]"; exit 999; }
               ^-- SC2015: Note that A && B || C is not if-then-else. C may run when A is true.
                                                                                                                     ^-^ SC2242: Can only exit with status 0-255. Other data should be written to stdout/stderr.
For more information: -- Use #!, not just #, for the sheba... -- Can only exit with status 0-255. ... -- Not following: /root/.keychain/ni...

Ah, let us fix line # 1:
vim +1 sshlogin
Update it as follows:


Save and close the file in vim/vi text editor. Try it again:
shellcheck sshlogin
shellcheck -x sshlogin

Here is sample code fixed:

. /home/scripts/.keychain/aws-ec2-server42-keys-sh
if [ "$1" != "" ]
     /usr/local/bin/rsnapshot "$1"
     echo "Usage: $0 [ahourly|bdaily|cmontly|dweekly|eyearly]"
     exit 1

Test it again:
shellcheck sshlogin
shellcheck -x sshlogin

Look ma, no more errors, and other problems. My script is now more readable and error-free. The -x follows source statements even when the file is not specified as input. By default, shellcheck will only follow files specified on the command line (plus /dev/null). This option allows following any file the script may source.

How to suppress SC1118 about the here-doc

We use the following syntax:
# shellcheck disable={NUMBER_HERE}
For instance:

# shellcheck disable=SC1118
cat << "eof"

How to set shell dialect

We can set Bourne shell dialect. Valid values are sh, bash, dash and ksh. The syntax is:
shellcheck -s bash myscript
shellcheck -s ksh /path/to/myscript

It is also possible to set up environment variable when you need default flags:

export SHELLCHECK_OPTS='--shell=dash --exclude=SC1118,SC1119'

We can tunr off color as follows:
shellcheck --color=never

I used the bat command (aliased to the cat) to produced colorized output for the sample script.

Summing up

That is all for now. Shellcheck has many more options. Do check the man page and see the project home page:
man shellcheck
I hope this simple and useful tip helps you write better scripts.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one

CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • Dave Jan 17, 2021 @ 20:42

    Is their a way to grammar check your headlines before you post them?

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @