ShellCheck helps find mistakes in your shell scripts before you run them. It points out errors and suggests fixes so your scripts work better.
ShellCheck for static analysis in Bash Scripting
Introduction
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
myscript.sh for errors and warnings.Bash Scripting
shellcheck myscript.sh
Bash Scripting
shellcheck -f gcc myscript.sh
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
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.
Practice
1. What is the main purpose of using
ShellCheck in shell scripting?easy
Solution
Step 1: Understand what ShellCheck does
ShellCheck is a tool that analyzes shell scripts without running them to find mistakes.Step 2: Identify the main benefit
It helps catch errors early, like missing quotes or bad syntax, making scripts safer.Final Answer:
To find errors and suggest fixes before running the script -> Option AQuick Check:
ShellCheck finds errors [OK]
Hint: ShellCheck checks scripts for errors before execution [OK]
Common Mistakes:
- Thinking ShellCheck runs the script
- Believing it speeds up script execution
- Confusing it with a code converter
2. Which of the following is the correct way to run ShellCheck on a script named
myscript.sh?easy
Solution
Step 1: Recall the ShellCheck command syntax
The correct command to run ShellCheck is simplyshellcheckfollowed by the script name.Step 2: Verify the options
Options B, C, and D use incorrect command names or extra words.Final Answer:
shellcheck myscript.sh -> Option DQuick Check:
Use 'shellcheck scriptname' [OK]
Hint: Use 'shellcheck' command followed by script name [OK]
Common Mistakes:
- Typing 'checkshell' instead of 'shellcheck'
- Adding extra words like 'run' before the command
- Using 'bash shellcheck' which is invalid
3. Given this script
echo Hello $name and running shellcheck script.sh, what warning will ShellCheck most likely show?medium
Solution
Step 1: Understand ShellCheck's common warnings
ShellCheck warns when variables are unquoted because it can cause word splitting or globbing.Step 2: Match warning to the script
The script uses$namewithout quotes, so ShellCheck will warn about missing quotes (SC2086).Final Answer:
SC2086: Double quote to prevent globbing and word splitting. -> Option CQuick Check:
Unquoted variables cause SC2086 warning [OK]
Hint: Look for unquoted variables to spot SC2086 warnings [OK]
Common Mistakes:
- Expecting syntax errors for simple echo
- Thinking no warning appears for unquoted variables
- Confusing warning codes with unrelated messages
4. You have this script snippet:
ShellCheck reports an error. What is the best fix?
if [ $count -eq 10 ]; then echo "Count is 10" fi
ShellCheck reports an error. What is the best fix?
medium
Solution
Step 1: Identify the ShellCheck warning cause
Using$countwithout quotes can cause errors if$countis empty or contains spaces.Step 2: Apply the fix
Adding quotes around$countensures the test works safely even if the variable is empty.Final Answer:
Add quotes around $count: if [ "$count" -eq 10 ]; then -> Option AQuick Check:
Quote variables in tests to avoid errors [OK]
Hint: Always quote variables inside [ ] tests [OK]
Common Mistakes:
- Removing spaces inside [ ] which breaks syntax
- Using == instead of -eq for numbers
- Adding unnecessary semicolons
5. You want to write a script that reads a filename from user input and safely prints its contents. Which ShellCheck advice should you follow?
hard
Solution
Step 1: Understand safe variable usage
Quoting variables prevents word splitting and globbing, which can cause errors or security issues.Step 2: Apply ShellCheck best practice
ShellCheck recommends quoting variables like"$filename"when used in commands likecat.Final Answer:
Always quote variables when using them, e.g.,cat "$filename"-> Option BQuick Check:
Quote variables to avoid unsafe expansions [OK]
Hint: Quote variables to prevent unsafe expansions [OK]
Common Mistakes:
- Using backticks which are outdated
- Not quoting variables causing errors
- Using eval which can be dangerous
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
