Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print the length of the array.
Bash Scripting
arr=(apple banana cherry)
echo ${#arr[1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ${#arr} which gives length of first element, not array length.
Using ${#arr[*]} which also works but '@' is preferred for clarity.
✗ Incorrect
In bash, ${#arr[@]} gives the number of elements in the array named arr.
2fill in blank
mediumComplete the code to assign the array length to a variable.
Bash Scripting
my_array=(dog cat mouse)
length=[1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using echo inside assignment which causes errors.
Using commands like count or length incorrectly.
✗ Incorrect
To assign the length of an array to a variable, use length=${#my_array[@]} without echo.
3fill in blank
hardFix the error in the code to correctly print the array length.
Bash Scripting
fruits=(orange lemon lime)
echo ${#fruits[1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using symbols like '#' or '&' inside brackets which cause errors.
Omitting brackets entirely.
✗ Incorrect
The correct syntax to get array length is ${#array[@]}. Using '@' inside brackets counts all elements.
4fill in blank
hardFill both blanks to create an array and print its length.
Bash Scripting
[1]=(red green blue) echo ${#colors[2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the array and length call.
Using '*' instead of '@' which also works but '@' is preferred.
✗ Incorrect
First, define the array with colors=(...). Then use ${#colors[@]} to get its length.
5fill in blank
hardFill all three blanks to create an array, assign its length to a variable, and print it.
Bash Scripting
[1]=(sun moon stars) len=[2] echo [3]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Forgetting the $ when printing the variable.
✗ Incorrect
Define array celestial=(...). Assign length with len=${#celestial[@]}. Print with echo $len.