ShellCheck – A Free Utility To Find Bugs In Your Shell Scripts

Find Bugs In Your Shell Scripts
Image source: Google.

I just wrote a script and wanted to check if there is any bugs in it. Is there any way to do it easily? Yes, of course! Now, you can easily find bugs in your shell scripts using ShellCheck. ShellCheck is a static analysis tool for shell scripts. It will tell you immediately if there is any error in your shell scripts instantly. It is written in Haskell language and freely distributed under GPLv3.

ShellCheck can do the following,

  • Check for incorrect quoting,
  • Check for incorrect test statements,
  • Recognize incorrect commands,
  • Recognize syntax errors,
  • Suggest you to improve script’s style,
  • Recognize typos,
  • Improve the robustness of your script,
  • And check few other issues.

You don’t need any expert’s help to rectify bugs in your scripts. Just open ShellCheck, paste your script and boom! You can use ShellCheck either online or offline. In this brief tutorial, I will show you both!

ShellCheck on Web

You can check the scripts on online at any by visiting the ShellCheck website.

Open up your web browser and go to the following link.

Paste your shell script in it for instant feedback.

Sample output:

 

Note: I didn’t write the script used in this example. I just found it online to test ShellCheck.

Alternatively, you can install it on your Linux box and test your script at anytime offline.

Install ShellCheck On Linux

ShellCheck is packaged for most Linux operating systems. You can easily install it using your distribution’s package manager as shown below.

On Arch Linux and its derivatives:

sudo pacman -S shellcheck

On Debian, Ubuntu, Linux Mint, run the following command to install it:

sudo apt-get install shellcheck

On RHEL, CentOS, Fedora systems:

sudo yum install

Or,

sudo dnf install ShellCheck

On openSUSE (Tumbleweed edition), run the following command as root user:

zypper in ShellCheck

Find Bugs In Your Shell Scripts

Once installed, open Terminal and run the following command to test your scripts.

shellcheck <path_of_your_script>

Example: 

shellcheck myscript.sh

Sample output:

In myscript.sh line 1:
# This script displays the date, time, username and
^-- SC2148: Tips depend on target shell and yours is unknown. Add a shebang.


In myscript.sh line 6:
 echo "Your username is: `whoami` \\n"
 ^-- SC2006: Use $(..) instead of legacy `..`.
 ^-- SC2028: echo won't expand escape sequences. Consider printf.

As you see in the above output, ShellCheck indicates the suggestions at line 1 and line 6. Please be mindful that ShellCheck won’t rectify scripts automatically, instead it will give warnings and suggestions for your shell scripts. This utility could be useful for budding script writers who wants to learn Shell scripting. Just write a script, copy/paste the code in ShellCheck and make it error-free in minutes. Give it a try. You won’t be disappointed.

Got any bugs in ShellCheck or want to suggest any feature improvements? Use the GitHub issue tracker.

Cheers!

Resource:

Thanks for stopping by!

How can I benefit from this blog:

Have a Good day!!

You may also like...