0
0
Bash Scriptingscripting~30 mins

ShellCheck for static analysis in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
ShellCheck for Static Analysis
📖 Scenario: You are a system administrator who wants to write a safe and error-free Bash script to automate a simple task. You will learn how to use ShellCheck, a tool that helps find mistakes in your Bash scripts before running them.
🎯 Goal: Create a Bash script that prints a greeting message. Then, use ShellCheck to find and fix a common mistake in the script. Finally, run the fixed script to see the correct output.
📋 What You'll Learn
Create a Bash script file named greet.sh with a greeting message.
Add a variable called name with the value World.
Use ShellCheck to find a warning about quoting the variable.
Fix the script by adding quotes around the variable.
Run the fixed script and print the greeting message.
💡 Why This Matters
🌍 Real World
System administrators and developers use ShellCheck to catch errors in Bash scripts before running them, saving time and avoiding bugs.
💼 Career
Knowing how to use ShellCheck is valuable for anyone writing or maintaining shell scripts in IT, DevOps, and software development roles.
Progress0 / 4 steps
1
Create the initial Bash script
Create a file named greet.sh and write a Bash script that sets a variable called name to World and prints Hello, $name! without quotes around $name.
Bash Scripting
Need a hint?

Remember to write the variable assignment without spaces around the equals sign. Use echo to print the message.

2
Run ShellCheck to find warnings
Run shellcheck greet.sh in the terminal to check the script. Add a comment in the script that says # shellcheck disable=SC2086 above the echo line to temporarily disable the warning about unquoted variables.
Bash Scripting
Need a hint?

Use the exact comment # shellcheck disable=SC2086 on the line before echo.

3
Fix the script by quoting the variable
Remove the shellcheck disable comment. Edit the echo line to add double quotes around $name like echo "Hello, $name!" to fix the warning properly.
Bash Scripting
Need a hint?

Put double quotes around $name inside the echo command to prevent word splitting.

4
Run the fixed script and print output
Run the fixed script ./greet.sh in the terminal and print the output. Write a command to run the script and a command to print the output.
Bash Scripting
Need a hint?

Make sure the script has execute permission with chmod +x greet.sh before running it.