ShellCheck is a static analysis tool designed for shell scripts. It helps identify common issues, errors, and potential bugs in scripts written in shell scripting languages such as Bash. ShellCheck analyzes the script and provides warnings or suggestions for improvement, making it easier to write clean, reliable, and maintainable shell scripts. This tutorial explains how to install ShellCheck on Ubuntu 24.04.
Install ShellCheck
Check the latest ShellCheck version from its GitHub repository and store it in a variable:
SHELLCHECK_VERSION=$(curl -s "https://api.github.com/repos/koalaman/shellcheck/releases/latest" | grep -Po '"tag_name": "v\K[0-9.]+')
Utilize the previously identified version to download the ShellCheck archive:
wget -qO shellcheck.tar.xz https://github.com/koalaman/shellcheck/releases/latest/download/shellcheck-v$SHELLCHECK_VERSION.linux.x86_64.tar.xz
Create temporary directory and extract a tar.xz
file:
mkdir shellcheck-temp
tar xf shellcheck.tar.xz --strip-components=1 -C shellcheck-temp
Move executable to /usr/local/bin
directory:
sudo mv shellcheck-temp/shellcheck /usr/local/bin
To determine the ShellCheck version, run this command:
shellcheck --version
Delete the unnecessary archive and directory:
rm -rf shellcheck.tar.xz shellcheck-temp
Testing ShellCheck
Create Bash script for testing purposes:
nano test.sh
Add the following code:
#!/bin/bash
test="Hello world"
echo $test
To use ShellCheck, run the shellcheck
command followed by the Bash script's name:
shellcheck test.sh
It will analyze the script and provide feedback, such as warning about unquoted variables, with suggestions on how to fix the issue and links to further information:
In test.sh line 4:
echo $test
^---^ SC2086 (info): Double quote to prevent globbing and word splitting.
Did you mean:
echo "$test"
For more information:
https://www.shellcheck.net/wiki/SC2086 -- Double quote to prevent globbing ...
Uninstall ShellCheck
To remove ShellCheck, delete its corresponding file:
sudo rm -rf /usr/local/bin/shellcheck
Leave a Comment
Cancel reply