0
0
Bash Scriptingscripting~20 mins

Why debugging saves hours in Bash Scripting - See It in Action

Choose your learning style9 modes available
Why debugging saves hours
📖 Scenario: You are writing a simple bash script to process a list of filenames. Sometimes, the script has errors that cause it to stop or behave unexpectedly. Debugging helps find and fix these errors quickly, saving hours of frustration.
🎯 Goal: Build a bash script step-by-step that reads a list of filenames, counts how many have a specific extension, and prints the count. You will add debugging steps to see how it helps find mistakes early.
📋 What You'll Learn
Create a list of filenames in a bash array
Add a variable for the file extension to count
Use a loop to count files with the given extension
Print the final count
Use debugging commands to check script behavior
💡 Why This Matters
🌍 Real World
Scripts like this help automate file management tasks, such as counting or processing files by type.
💼 Career
Debugging skills are essential for any scripting or automation job to quickly fix errors and improve script reliability.
Progress0 / 4 steps
1
Create a list of filenames
Create a bash array called files with these exact filenames: report.txt, image.png, notes.txt, data.csv, summary.txt
Bash Scripting
Need a hint?

Use parentheses to create an array and double quotes for each filename.

2
Add a file extension variable
Add a variable called ext and set it to the string txt to represent the file extension to count.
Bash Scripting
Need a hint?

Use simple assignment without spaces around the equal sign.

3
Count files with the extension
Use a for loop with variable file to iterate over ${files[@]}. Inside the loop, use an if statement to check if $file ends with .$ext. If yes, increment a counter variable called count. Initialize count to 0 before the loop.
Bash Scripting
Need a hint?

Use double square brackets and wildcard * to check the file extension.

4
Print the count with debugging
Add a set -x command before the loop to enable debugging. After the loop, add set +x to disable debugging. Finally, print the message Number of .$ext files: $count using echo.
Bash Scripting
Need a hint?

The set -x command shows each command as it runs. Use set +x to stop it.