Recall & Review
beginner
What is an array in Bash scripting?
An array in Bash is a list of values stored in a single variable. You can access each value by its position number, starting at zero.
Click to reveal answer
beginner
How do you write a simple for loop to iterate over all elements in a Bash array named
fruits?Use: <br>
for fruit in "${fruits[@]}"; do<br> echo "$fruit"<br>done<br>This prints each fruit in the array one by one.Click to reveal answer
beginner
What does
${array[@]} mean in Bash?It means all elements of the array. When used in a loop, it lets you access each item one by one.
Click to reveal answer
beginner
How can you get the number of elements in a Bash array named
colors?Use
${#colors[@]}. It returns the count of items in the array.Click to reveal answer
intermediate
Why is it important to quote
"${array[@]}" when iterating over arrays in Bash?Quoting preserves each element as a single item, even if it contains spaces. Without quotes, elements with spaces may split into multiple words.
Click to reveal answer
Which syntax correctly iterates over all elements in a Bash array named
items?✗ Incorrect
Option A correctly uses quotes and
${items[@]} to iterate over all array elements safely.How do you get the length of a Bash array named
my_array?✗ Incorrect
In Bash,
${#array[@]} returns the number of elements in the array.What happens if you forget to quote
"${array[@]}" in a for loop?✗ Incorrect
Without quotes, elements containing spaces split into separate words, causing unexpected behavior.
Which of these is a valid way to declare an array in Bash?
✗ Incorrect
Option D uses the correct Bash syntax for array declaration.
How do you access the first element of a Bash array named
list?✗ Incorrect
Bash arrays start at index 0, so
${list[0]} accesses the first element.Explain how to iterate over all elements in a Bash array and print each element.
Think about how you loop over a list of items in real life, like reading names from a list.
You got /4 concepts.
Describe why quoting array expansions is important when iterating over arrays in Bash.
Imagine reading a list where some names have spaces, like 'New York'.
You got /3 concepts.