Bird
0
0

What does the bash expression ${var/old/new} do?

easy🧠 Conceptual Q1 of 15
Bash Scripting - String Operations
What does the bash expression ${var/old/new} do?
AReplaces the first occurrence of 'old' with 'new' in the variable 'var'
BReplaces all occurrences of 'old' with 'new' in the variable 'var'
CDeletes the variable 'var' if it contains 'old'
DPrints the variable 'var' without any changes
Step-by-Step Solution
Solution:
  1. Step 1: Understand the syntax ${var/old/new}

    This syntax replaces only the first occurrence of 'old' with 'new' in the string stored in 'var'.
  2. Step 2: Differentiate from global replacement

    Using double slashes like ${var//old/new} replaces all occurrences, but single slash replaces only the first.
  3. Final Answer:

    Replaces the first occurrence of 'old' with 'new' in the variable 'var' -> Option A
  4. Quick Check:

    String replacement first occurrence = C [OK]
Quick Trick: Single slash replaces first occurrence only [OK]
Common Mistakes:
MISTAKES
  • Confusing single slash with double slash
  • Thinking it replaces all occurrences
  • Assuming it deletes text
  • Assuming no change occurs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes