0
0
Bash Scriptingscripting~20 mins

Why arrays handle lists of data in Bash Scripting - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Mastery in Bash
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Bash array script?
Consider the following Bash script that uses arrays. What will it print?
Bash Scripting
arr=(apple banana cherry)
echo ${arr[1]}
Abanana
BSyntaxError
Ccherry
Dapple
Attempts:
2 left
💡 Hint
Remember that Bash arrays start counting from zero.
🧠 Conceptual
intermediate
2:00remaining
Why use arrays instead of separate variables in Bash?
Which reason best explains why arrays are useful for handling lists of data in Bash?
AArrays let you store multiple related values under one name with indexes.
BArrays automatically sort data alphabetically.
CArrays prevent any data from being changed once set.
DArrays allow you to run commands faster than variables.
Attempts:
2 left
💡 Hint
Think about how you would keep track of many items easily.
🔧 Debug
advanced
2:00remaining
Identify the error in this Bash array usage
What error will this script produce? arr=(one two three) echo ${arr[3]}
APrints 'three' because arrays wrap around
BSyntaxError due to invalid index
CPrints an empty line because index 3 does not exist
DPrints 'one' because it loops back to start
Attempts:
2 left
💡 Hint
Check the array length and valid indexes.
🚀 Application
advanced
2:00remaining
How to loop over all elements in a Bash array?
Which script correctly prints each element of the array 'fruits' on its own line?
Bash Scripting
fruits=(apple banana cherry)
Afor fruit in ${fruits}; do echo $fruit; done
Bfor fruit in "${fruits[@]}"; do echo $fruit; done
Cfor fruit in ${fruits[*]}; do echo $fruit; done
Dfor fruit in fruits; do echo $fruit; done
Attempts:
2 left
💡 Hint
Use the syntax that preserves each array element as a separate word.
📝 Syntax
expert
2:00remaining
Which option correctly declares and prints a Bash array element?
Select the option that correctly creates an array with elements 'red', 'green', 'blue' and prints the second element.
A
colors={red green blue}
echo ${colors[2]}
B
colors=[red, green, blue]
echo ${colors[1]}
C
colors=(red, green, blue)
echo ${colors[1]}
D
colors=(red green blue)
echo ${colors[1]}
Attempts:
2 left
💡 Hint
Remember the correct syntax for Bash arrays uses parentheses without commas.