Bird
0
0

Which of the following is the correct syntax to replace the first occurrence of 'cat' with 'dog' in variable animal?

easy📝 Syntax Q12 of 15
Bash Scripting - String Operations
Which of the following is the correct syntax to replace the first occurrence of 'cat' with 'dog' in variable animal?
A${animal/cat/dog}
B${animal//cat/dog}
C${animal/cat|dog}
D${animal:cat:dog}
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct replacement syntax

    The correct syntax for replacing the first occurrence is ${variable/old/new}, so ${animal/cat/dog} is correct.
  2. Step 2: Check other options for errors

    ${animal//cat/dog} uses double slash which replaces all occurrences, not just the first. Options C and D use invalid syntax.
  3. Final Answer:

    ${animal/cat/dog} -> Option A
  4. Quick Check:

    Single slash correct for first occurrence [OK]
Quick Trick: Use single slash for first replacement, double slash for all [OK]
Common Mistakes:
MISTAKES
  • Using double slash when only first replacement is needed
  • Using wrong delimiters like | or : instead of /
  • Confusing syntax with substring extraction

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes