Bird
0
0

What is the correct syntax to access the second element of a Bash array named arr?

easy📝 Syntax Q3 of 15
Bash Scripting - Arrays
What is the correct syntax to access the second element of a Bash array named arr?
Aecho ${arr(2)}
Becho ${arr[1]}
Cecho ${arr[2]}
Decho ${arr.2}
Step-by-Step Solution
Solution:
  1. Step 1: Remember Bash array indexing

    Bash arrays start at index 0, so the second element is at index 1.
  2. Step 2: Use correct syntax for element access

    The syntax ${arr[1]} accesses the element at index 1 correctly.
  3. Final Answer:

    echo ${arr[1]} -> Option B
  4. Quick Check:

    Second element = ${arr[1]} [OK]
Quick Trick: Array indexing starts at 0; second element is index 1 [OK]
Common Mistakes:
MISTAKES
  • Using parentheses instead of brackets
  • Using 1-based indexing like ${arr[2]}
  • Using dot notation which is invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes