Bird
0
0

Which of the following is the correct syntax to extract 5 characters starting at position 2 from variable var in bash?

easy📝 Syntax Q3 of 15
Bash Scripting - String Operations
Which of the following is the correct syntax to extract 5 characters starting at position 2 from variable var in bash?
A${var:2:5}
B${var:5:2}
C${var:2-5}
D${var(2,5)}
Step-by-Step Solution
Solution:
  1. Step 1: Recall substring syntax

    The correct syntax is ${var:offset:length} where offset and length are numbers.
  2. Step 2: Match syntax to options

    ${var:2:5} uses ${var:2:5} which means start at index 2 and take 5 characters, matching the requirement.
  3. Final Answer:

    ${var:2:5} -> Option A
  4. Quick Check:

    Correct substring syntax = ${var:2:5} [OK]
Quick Trick: Use colon-separated offset and length inside braces [OK]
Common Mistakes:
MISTAKES
  • Using parentheses instead of braces
  • Swapping offset and length
  • Using minus sign instead of colon

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes