Bird
0
0

Why does ${var/old/new} replace only the first occurrence of 'old' and not all?

hard🧠 Conceptual Q10 of 15
Bash Scripting - String Operations
Why does ${var/old/new} replace only the first occurrence of 'old' and not all?
ABecause single slash syntax is designed to replace only the first match
BBecause bash does not support global replacement
CBecause 'new' must be repeated for each occurrence
DBecause variable expansion is disabled by default
Step-by-Step Solution
Solution:
  1. Step 1: Understand bash string replacement design

    The single slash syntax ${var/old/new} is intentionally designed to replace only the first occurrence.
  2. Step 2: Contrast with double slash syntax

    Using double slashes ${var//old/new} replaces all occurrences, showing bash supports global replacement.
  3. Final Answer:

    Because single slash syntax is designed to replace only the first match -> Option A
  4. Quick Check:

    Single slash = first occurrence only = A [OK]
Quick Trick: Single slash replaces first match; double slash replaces all [OK]
Common Mistakes:
MISTAKES
  • Thinking bash lacks global replacement
  • Assuming multiple replacements need repeated new strings
  • Confusing variable expansion settings
  • Misunderstanding syntax purpose

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes