Challenge - 5 Problems
Quoting Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this script snippet?
Consider this Bash script snippet:
What will happen when you run it?
filename=My Documents/file.txt cat $filename
What will happen when you run it?
Bash Scripting
filename=My Documents/file.txt cat $filename
Attempts:
2 left
💡 Hint
Think about how Bash treats spaces in unquoted variables.
✗ Incorrect
Without quotes, Bash splits $filename at spaces, so 'cat' tries to open 'My' and 'Documents/file.txt' separately. Since 'My' doesn't exist, it errors.
💻 Command Output
intermediate2:00remaining
How does quoting affect command arguments?
What is the output of this script?
Assuming 'My Documents' is a directory, what happens?
dir="My Documents" ls $dir ls "$dir"
Assuming 'My Documents' is a directory, what happens?
Bash Scripting
dir="My Documents" ls $dir ls "$dir"
Attempts:
2 left
💡 Hint
Consider how unquoted variables split on spaces.
✗ Incorrect
Unquoted $dir splits into two arguments 'My' and 'Documents', causing ls to look for 'My' which fails. Quoted "$dir" passes the full directory name as one argument.
🔧 Debug
advanced3:00remaining
Why does this script fail to handle filenames with spaces?
This script tries to copy all .txt files to backup folder:
It fails on files with spaces. Why?
for file in *.txt; do cp $file backup/ done
It fails on files with spaces. Why?
Bash Scripting
for file in *.txt; do cp $file backup/ done
Attempts:
2 left
💡 Hint
Think about how Bash treats variables with spaces when unquoted.
✗ Incorrect
Unquoted $file splits filenames with spaces into separate words, causing cp to receive wrong arguments and fail.
🚀 Application
advanced3:00remaining
Fix the script to safely handle filenames with spaces
Given this script:
It breaks on filenames with spaces. Which option fixes it?
files=$(ls *.txt) for f in $files; do echo "$f" done
It breaks on filenames with spaces. Which option fixes it?
Bash Scripting
files=$(ls *.txt) for f in $files; do echo "$f" done
Attempts:
2 left
💡 Hint
Avoid parsing ls output; use globbing directly.
✗ Incorrect
Using 'for f in *.txt' iterates over filenames safely, preserving spaces. Parsing ls output breaks on spaces.
🧠 Conceptual
expert2:30remaining
Why is quoting variables critical in Bash scripting?
Which statement best explains why quoting variables prevents errors in Bash scripts?
Attempts:
2 left
💡 Hint
Think about what happens when variables contain spaces or wildcards.
✗ Incorrect
Quoting stops Bash from splitting variables into multiple words or expanding wildcards, which avoids many common errors.