0
0
Bash Scriptingscripting~10 mins

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

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an array named 'fruits' with three items.

Bash Scripting
fruits=([1])
Drag options to blanks, or click blank then click option'
A"apple" "banana" "cherry"
B"apple, banana, cherry"
Capple banana cherry
D[apple banana cherry]
Attempts:
3 left
💡 Hint
Common Mistakes
Using commas between items inside the array.
Putting the entire list inside quotes as one string.
Using square brackets instead of parentheses.
2fill in blank
medium

Complete the code to print the second item in the 'fruits' array.

Bash Scripting
echo ${fruits[1]
Drag options to blanks, or click blank then click option'
A[2]
B[1]
C[0]
D[3]
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 2 to get the second item.
Forgetting to use curly braces around the variable.
Using parentheses instead of square brackets.
3fill in blank
hard

Fix the error in the code to add 'orange' to the end of the 'fruits' array.

Bash Scripting
fruits+=([1])
Drag options to blanks, or click blank then click option'
A"orange"
Borange
C'orange'
D[orange]
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes causing syntax errors.
Using square brackets which are invalid here.
Using single quotes which also work but double quotes are preferred.
4fill in blank
hard

Fill both blanks to loop over the 'fruits' array and print each item.

Bash Scripting
for [1] in "${fruits[2]"; do
  echo $[1]
done
Drag options to blanks, or click blank then click option'
Afruit
B[@]
C[*]
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using [*] which treats all items as one string.
Using invalid variable names.
Forgetting quotes around the array expansion.
5fill in blank
hard

Fill all three blanks to create an associative array 'colors' with keys and values, then print the value for key 'apple'.

Bash Scripting
declare -A colors=([1])
echo ${colors[2]
colors[banana]=[3]
Drag options to blanks, or click blank then click option'
A[apple]=red [banana]=yellow
B[apple]
Cgreen
D[banana]
Attempts:
3 left
💡 Hint
Common Mistakes
Not using declare -A for associative arrays.
Using parentheses without brackets for keys.
Forgetting to use brackets when accessing or assigning.