Bird
0
0

Which of the following is the correct syntax to print the third element of an array colors in Bash?

easy📝 Syntax Q12 of 15
Bash Scripting - Arrays
Which of the following is the correct syntax to print the third element of an array colors in Bash?
Aecho ${colors(3)}
Becho $colors[3]
Cecho ${colors[2]}
Decho colors[2]
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct index for third element

    Since indexing starts at 0, the third element is at index 2.
  2. Step 2: Use proper Bash array syntax

    The correct syntax is ${array_name[index]}. So, ${colors[2]} is correct.
  3. Final Answer:

    echo ${colors[2]} -> Option C
  4. Quick Check:

    Third element = ${colors[2]} [OK]
Quick Trick: Use zero-based index and ${array[index]} syntax [OK]
Common Mistakes:
MISTAKES
  • Using parentheses instead of brackets
  • Using 1-based index (3 instead of 2)
  • Missing ${} around array access
  • Printing array name as string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes