📖 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
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
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
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
Hint
The set -x command shows each command as it runs. Use set +x to stop it.
Practice
(1/5)
1. What is the main benefit of using debugging in bash scripts?
easy
A. It helps find errors quickly by showing commands as they run
B. It makes the script run faster
C. It automatically fixes syntax errors
D. It hides all output to keep the terminal clean
Solution
Step 1: Understand debugging purpose
Debugging shows each command as it runs, helping spot where errors happen.
Step 2: Compare options
Only It helps find errors quickly by showing commands as they run describes this benefit; others are incorrect or unrelated.
Final Answer:
It helps find errors quickly by showing commands as they run -> Option A
Quick Check:
Debugging = Find errors fast [OK]
Hint: Debugging shows commands step-by-step to spot errors fast [OK]
Common Mistakes:
Thinking debugging speeds up script execution
Believing debugging fixes errors automatically
Assuming debugging hides output
2. Which command enables debugging mode in a bash script?
easy
A. bash --quiet
B. set +x
C. set -x
D. bash -c
Solution
Step 1: Recall debugging syntax
The command set -x turns on debugging to show commands as they run.
Step 2: Eliminate wrong options
set +x disables debugging; others do not enable debugging.
Final Answer:
set -x -> Option C
Quick Check:
Enable debugging = set -x [OK]
Hint: Use 'set -x' to start debugging in bash scripts [OK]
Common Mistakes:
Using 'set +x' which disables debugging
Confusing bash options like '--quiet' or '-c'
Not knowing debugging commands
3. What will be the output of this script with debugging enabled?
#!/bin/bash
set -x
name="Alice"
echo "Hello, $name!"
medium
A. Hello, Alice!
B. Error: variable not set
C. + echo Hello, $name!
Hello, $name!
D. + name=Alice
+ echo 'Hello, Alice!'
Hello, Alice!
Solution
Step 1: Understand debugging output
With set -x, bash prints each command with a '+' before running it.
Step 2: Trace script commands
It shows name=Alice and echo 'Hello, Alice!' before printing the greeting.