0
0
Bash Scriptingscripting~20 mins

Why quoting rules prevent errors in Bash Scripting - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Quoting Mastery Badge
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:
filename=My Documents/file.txt
cat $filename

What will happen when you run it?
Bash Scripting
filename=My Documents/file.txt
cat $filename
Acat: My: No such file or directory
BContents of 'My Documents/file.txt' are displayed
CSyntax error due to spaces in variable assignment
Dcat: file.txt: No such file or directory
Attempts:
2 left
💡 Hint
Think about how Bash treats spaces in unquoted variables.
💻 Command Output
intermediate
2:00remaining
How does quoting affect command arguments?
What is the output of this script?
dir="My Documents"
ls $dir
ls "$dir"

Assuming 'My Documents' is a directory, what happens?
Bash Scripting
dir="My Documents"
ls $dir
ls "$dir"
ABoth ls commands list contents of 'My Documents'
BFirst ls fails with 'No such file or directory', second ls lists contents
CBoth ls commands fail with 'No such file or directory'
DFirst ls lists contents, second ls fails
Attempts:
2 left
💡 Hint
Consider how unquoted variables split on spaces.
🔧 Debug
advanced
3:00remaining
Why does this script fail to handle filenames with spaces?
This script tries to copy all .txt files to backup folder:
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
ABecause $file is unquoted, so filenames with spaces split into multiple arguments
BBecause the for loop syntax is incorrect
CBecause backup/ folder does not exist
DBecause cp command requires -r option for files
Attempts:
2 left
💡 Hint
Think about how Bash treats variables with spaces when unquoted.
🚀 Application
advanced
3:00remaining
Fix the script to safely handle filenames with spaces
Given this script:
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
A
Use: files=$(ls *.txt)
for f in "$files"; do echo "$f"; done
B
Use: IFS=$'\n'
files=$(ls *.txt)
for f in $files; do echo "$f"; done
CUse: for f in *.txt; do echo "$f"; done
D
Use: files=$(ls *.txt | tr '\n' ' ')
for f in $files; do echo "$f"; done
Attempts:
2 left
💡 Hint
Avoid parsing ls output; use globbing directly.
🧠 Conceptual
expert
2:30remaining
Why is quoting variables critical in Bash scripting?
Which statement best explains why quoting variables prevents errors in Bash scripts?
AQuoting variables makes scripts run faster by disabling shell expansions
BQuoting variables is only needed when variables contain numbers
CQuoting variables automatically escapes all special characters inside the variable
DQuoting variables prevents word splitting and glob expansion, ensuring arguments with spaces or special characters are handled correctly
Attempts:
2 left
💡 Hint
Think about what happens when variables contain spaces or wildcards.