Bird
0
0

Which of the following is the correct way to declare an indexed array named pets with elements dog, cat, and bird in Bash?

easy🧠 Conceptual Q1 of 15
Bash Scripting - Arrays
Which of the following is the correct way to declare an indexed array named pets with elements dog, cat, and bird in Bash?
Apets=[dog, cat, bird]
Bpets=(dog cat bird)
Cpets={dog, cat, bird}
Dpets=("dog", "cat", "bird")
Step-by-Step Solution
Solution:
  1. Step 1: Understand Bash array declaration syntax

    Bash indexed arrays are declared using parentheses with space-separated values, without commas or braces.
  2. Step 2: Check each option for correct syntax

    pets=(dog cat bird) uses parentheses and space-separated values correctly. pets=[dog, cat, bird] uses square brackets and commas, which is invalid. pets={dog, cat, bird} uses curly braces, which is invalid. pets=("dog", "cat", "bird") uses commas inside parentheses, which is invalid in Bash.
  3. Final Answer:

    pets=(dog cat bird) -> Option B
  4. Quick Check:

    Bash indexed array declaration = pets=(dog cat bird) [OK]
Quick Trick: Use parentheses with spaces, no commas for Bash arrays [OK]
Common Mistakes:
MISTAKES
  • Using commas between elements
  • Using square or curly brackets instead of parentheses
  • Quoting elements unnecessarily in simple cases

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes