0
0
Bash Scriptingscripting~20 mins

Why debugging saves hours in Bash Scripting - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Debugging Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this script snippet?
Consider this bash script snippet that tries to list files and count them:
files=$(ls /tmp)
echo $files
count=$(echo $files | wc -w)
echo $count
What will be the output if /tmp contains exactly three files named a.txt, b.txt, and c.txt?
Bash Scripting
files=$(ls /tmp)
echo $files
count=$(echo $files | wc -w)
echo $count
A
a.txt b.txt c.txt
3
B
a.txt
b.txt
c.txt
3
C
a.txt b.txt c.txt
1
D
a.txt
b.txt
c.txt
1
Attempts:
2 left
💡 Hint
Think about how command substitution and word splitting work in bash.
🔧 Debug
intermediate
2:00remaining
Why does this script fail to count files correctly?
This script tries to count the number of files in /var/log:
count=$(ls /var/log | wc -l)
echo "There are $count files."
But it sometimes gives wrong counts or errors. Why?
Bash Scripting
count=$(ls /var/log | wc -l)
echo "There are $count files."
ABecause 'ls' may output color codes that wc counts as extra lines.
BBecause 'ls' outputs filenames with newlines, so wc -l counts lines correctly.
CBecause some filenames have spaces, causing ls output to be split incorrectly.
DBecause 'ls' can output directories and files, so count is not just files.
Attempts:
2 left
💡 Hint
ls lists everything in the directory: files, directories, symlinks, etc. Does it filter to files only?
🚀 Application
advanced
2:00remaining
Which script correctly counts files including those with spaces?
You want to count all files in /home/user/docs, including those with spaces in names. Which script correctly does this?
A
count=$(find /home/user/docs -type f | wc -l)
echo $count
B
count=$(ls /home/user/docs | wc -l)
echo $count
C
count=$(find /home/user/docs -type f -print0 | wc -l)
echo $count
D
count=$(ls -1 /home/user/docs | wc -w)
echo $count
Attempts:
2 left
💡 Hint
Think about which command handles spaces safely and counts files correctly.
🧠 Conceptual
advanced
2:00remaining
Why is debugging important in scripting automation?
Which statement best explains why debugging saves hours in scripting and automation?
ABecause debugging allows scripts to run faster by optimizing code automatically.
BBecause debugging removes the need for testing scripts before use.
CBecause debugging helps find and fix errors early, preventing bigger problems later.
DBecause debugging replaces the need for writing comments and documentation.
Attempts:
2 left
💡 Hint
Think about the role of debugging in catching mistakes.
📝 Syntax
expert
2:00remaining
What error does this bash script produce?
What error will this script produce?
for file in $(ls /etc/passwd); do
  echo $file
 done
Bash Scripting
for file in $(ls /etc/passwd); do
  echo $file
 done
ASyntaxError due to missing semicolon after 'do'.
BNo error, prints '/etc/passwd'.
CRuntime error: 'ls' cannot list a file, only directories.
DNo output because the loop runs zero times.
Attempts:
2 left
💡 Hint
Think about what 'ls /etc/passwd' outputs and how the for loop processes it.