Bird
0
0

What is wrong with this code snippet?

medium📝 Debug Q14 of 15
Bash Scripting - String Operations
What is wrong with this code snippet?
file="data.txt"
newfile=${file/data/txt}
echo $newfile
AIt replaces 'data' with 'txt' correctly.
BSyntax error due to missing slashes in replacement.
CIt replaces 'data' with 'txt' but misses the dot.
DIt replaces 'data' with 'txt' but changes the file extension incorrectly.
Step-by-Step Solution
Solution:
  1. Step 1: Check the replacement syntax

    The replacement syntax must be ${var/old/new} with slashes separating parts. Here, ${file/data/txt} is correct syntax.
  2. Step 2: Analyze the replacement effect

    The code replaces 'data' with 'txt', so 'data.txt' becomes 'txt.txt'. There is no syntax error.
  3. Step 3: Identify the logical issue

    While the code executes correctly, the replacement of 'data' with 'txt' produces 'txt.txt', which alters the filename in a way that incorrectly impacts the intended file extension handling.
  4. Final Answer:

    It replaces 'data' with 'txt' but changes the file extension incorrectly. -> Option D
  5. Quick Check:

    Replacement changes 'data' to 'txt', affecting filename [OK]
Quick Trick: Check slashes and replaced parts carefully for syntax and effect [OK]
Common Mistakes:
MISTAKES
  • Assuming syntax error when slashes are correct
  • Ignoring that replacement changes filename parts
  • Confusing replacement with substring extraction

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes