An indexed array in Bash is a list of values stored in a single variable, where each value has a numeric index starting from 0.
Click to reveal answer
beginner
How do you declare an indexed array with values in Bash?
Use syntax: array_name=(value1 value2 value3). For example: fruits=(apple banana cherry).
Click to reveal answer
beginner
How do you access the second element of an indexed array named colors?
Use ${colors[1]} because indexing starts at 0.
Click to reveal answer
intermediate
How to add a new element to an existing indexed array in Bash?
Use syntax: array_name+=(new_value). For example: fruits+=('orange') adds 'orange' to the fruits array.
Click to reveal answer
beginner
What command shows all elements of an indexed array named my_array?
Use echo ${my_array[@]} to print all elements separated by spaces.
Click to reveal answer
How do you declare an indexed array named pets with elements 'dog', 'cat', and 'bird'?
Apets<dog cat bird>
Bpets=[dog, cat, bird]
Cpets=(dog cat bird)
Dpets={dog cat bird}
✗ Incorrect
In Bash, indexed arrays are declared with parentheses and space-separated values.
What is the index of the first element in a Bash indexed array?
A1
B-1
CDepends on the array
D0
✗ Incorrect
Bash arrays start indexing at 0, like many programming languages.
How do you print all elements of an indexed array colors?
Aecho ${colors[@]}
Becho ${colors[*]}
Cecho colors
Decho colors[]
✗ Incorrect
Both ${colors[@]} and ${colors[*]} print all elements, but ${colors[@]} is preferred for separate elements.
How to add 'pear' to an existing array fruits?
Afruits.append('pear')
Bfruits+=('pear')
Cfruits.push('pear')
Dfruits = fruits + 'pear'
✗ Incorrect
In Bash, use += with parentheses to add elements to an array.
Which syntax is correct to access the third element of array numbers?
A${numbers[2]}
B${numbers[3]}
C${numbers(2)}
D${numbers{2}}
✗ Incorrect
Indexing starts at 0, so the third element is at index 2.
Explain how to declare and access elements in an indexed array in Bash.
Think about how you list items in a shopping list and then pick one by its position.
You got /4 concepts.
Describe how to add a new element to an existing indexed array in Bash.
Imagine adding a new item to your list without rewriting the whole list.
You got /4 concepts.
Practice
(1/5)
1. What is the correct way to declare an indexed array named fruits with values apple, banana, and cherry in Bash?
easy
A. fruits=[apple, banana, cherry]
B. fruits=("apple, banana, cherry")
C. fruits={apple banana cherry}
D. fruits=(apple banana cherry)
Solution
Step 1: Understand Bash array declaration syntax
Bash arrays use parentheses with space-separated values, no commas or braces.
Step 2: Check each option's syntax
fruits=(apple banana cherry) uses parentheses and spaces correctly. Square brackets and braces are incorrect. fruits=("apple, banana, cherry") treats all values as one string.