0
0
Bash Scriptingscripting~5 mins

ShellCheck for static analysis in Bash Scripting

Choose your learning style9 modes available
Introduction

ShellCheck helps find mistakes in your shell scripts before you run them. It points out errors and suggests fixes so your scripts work better.

Before running a new shell script to catch typos or syntax errors.
When you want to improve an existing script and make it safer.
To learn better shell scripting habits by seeing suggestions.
When sharing scripts with others to ensure they are error-free.
Syntax
Bash Scripting
shellcheck [options] script.sh

Replace script.sh with your script file name.

You can run ShellCheck from the command line on your computer.

Examples
Check the script named myscript.sh for errors and warnings.
Bash Scripting
shellcheck myscript.sh
Show output in GCC style format, useful for some editors.
Bash Scripting
shellcheck -f gcc myscript.sh
Ignore warning SC2086 (word splitting) for this run.
Bash Scripting
shellcheck -e SC2086 myscript.sh
Sample Program

This script tries to read a file but forgets to quote the variable. ShellCheck will warn about this.

Bash Scripting
#!/bin/bash

# Example script with a common mistake
filename="some file.txt"
cat $filename

# Run ShellCheck on this script to see the warning
OutputSuccess
Important Notes

Always fix warnings about quoting variables to avoid bugs.

ShellCheck can be integrated into many code editors for instant feedback.

It works offline and does not run your script, so it is safe to use.

Summary

ShellCheck finds errors in shell scripts before running them.

Use it to catch common mistakes like missing quotes or wrong syntax.

It helps write safer and more reliable shell scripts.