Bird
0
0

Given the script snippet: filename=My File.txt rm $filename What is the main problem and how to fix it?

medium📝 Debug Q14 of 15
Bash Scripting - Quoting and Expansion
Given the script snippet: filename=My File.txt rm $filename What is the main problem and how to fix it?
ANo problem; script works fine
Brm command will fail with syntax error; fix by escaping spaces
Crm command will delete the wrong file; fix by quoting: rm "$filename"
Drm command will delete all files; fix by using single quotes
Step-by-Step Solution
Solution:
  1. Step 1: Identify the issue with unquoted variable containing spaces

    Without quotes, rm sees two arguments: 'My' and 'File.txt', causing errors or deleting wrong files.
  2. Step 2: Fix by quoting the variable

    Using rm "$filename" treats the whole filename as one argument, preventing errors.
  3. Final Answer:

    rm command will delete the wrong file; fix by quoting: rm "$filename" -> Option C
  4. Quick Check:

    Quote filenames with spaces to avoid rm errors = D [OK]
Quick Trick: Quote filenames with spaces to avoid command errors [OK]
Common Mistakes:
MISTAKES
  • Not quoting variables with spaces
  • Using single quotes prevents variable expansion
  • Assuming no error occurs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes