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.
Download Free Guide: “Advanced Bash-Scripting Guide”
ShellCheck can do the following,
Check forincorrect quoting, Check forincorrect 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 WebYou 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 LinuxShellCheck 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 shellcheckOn Debian, Ubuntu, Linux Mint, run the following command to install it:
sudo apt-get install shellcheckOn RHEL, CentOS, Fedora systems:
sudo yum installOr,
sudo dnf install ShellCheckOn openSUSE (Tumbleweed edition), run the following command as root user:
zypper in ShellCheck Find Bugs In Your Shell ScriptsOnce 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.
https://github.com/koalaman/shellcheck/issuesCheers!
Resource: