Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
In bash, arrays are created by listing items separated by spaces inside parentheses without quotes around the whole list.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
Array indexes in bash start at 0, so the second item is at index 1.
3fill in blank
hardFix 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'
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.
✗ Incorrect
When adding a string with special characters or spaces, quotes are needed. Here, double quotes are preferred.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using [*] which treats all items as one string.
Using invalid variable names.
Forgetting quotes around the array expansion.
✗ Incorrect
Use a variable name like 'fruit' for each item, and "${fruits[@]}" to loop over all items individually.
5fill in blank
hardFill 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'
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.
✗ Incorrect
Associative arrays use declare -A and key-value pairs in brackets. To print a value, use the key in brackets. To assign, use the key in brackets and set the value.