Recall & Review
beginner
How do you define an array in Bash?
You define an array by listing elements inside parentheses, separated by spaces. For example:
my_array=(apple banana cherry)Click to reveal answer
beginner
How do you access the first element of an array named
my_array in Bash?Use the syntax
${my_array[0]} to get the first element because array indexes start at 0.Click to reveal answer
beginner
What does
${my_array[@]} do in Bash?It expands to all elements of the array
my_array. Useful to loop over or print all items.Click to reveal answer
beginner
How can you get the number of elements in a Bash array
my_array?Use
${#my_array[@]} to get the count of elements in the array.Click to reveal answer
beginner
What happens if you try to access an array element with an index that does not exist?
You get an empty string because the element is not set. Bash does not throw an error for out-of-range indexes.
Click to reveal answer
In Bash, how do you access the third element of an array named
fruits?✗ Incorrect
Array indexes start at 0, so the third element is at index 2.
What does
${my_array[@]} return in Bash?✗ Incorrect
The
@ symbol expands to all elements in the array.How do you find the number of elements in a Bash array
colors?✗ Incorrect
The syntax
${#array[@]} returns the count of elements.What will happen if you access
${my_array[10]} but the array has only 3 elements?✗ Incorrect
Bash returns an empty string for out-of-range indexes without error.
Which of these is the correct way to define an array in Bash?
✗ Incorrect
Arrays are defined with parentheses and space-separated elements.
Explain how to access elements in a Bash array and how indexing works.
Think about how you pick items from a list starting at zero.
You got /3 concepts.
Describe how to get all elements and the length of a Bash array.
Consider how you might count or list all items in a shopping list.
You got /3 concepts.