Bird
0
0

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

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

    This syntax replaces only the first occurrence of the substring 'old' with 'new' inside the variable 'var'.
  2. Step 2: Differentiate from ${var//old/new}

    The double slash version replaces all occurrences, but the single slash replaces only the first one.
  3. Final Answer:

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

    Single slash means first occurrence only [OK]
Quick Trick: Single slash replaces first match only, double slash replaces all [OK]
Common Mistakes:
MISTAKES
  • Confusing single slash with double slash for all replacements
  • Thinking it deletes or appends instead of replacing
  • Assuming it works on strings outside variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes