0
0
Bash Scriptingscripting~10 mins

Array length in Bash Scripting - Interactive Code Practice

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

Complete 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'
A[@]
B[*]
C[&]
D[#]
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.
2fill in blank
medium

Complete 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'
Alength ${#my_array[@]}
Becho ${#my_array[@]}
Ccount ${#my_array[@]}
D${#my_array[@]}
Attempts:
3 left
💡 Hint
Common Mistakes
Using echo inside assignment which causes errors.
Using commands like count or length incorrectly.
3fill in blank
hard

Fix 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'
A[@]
B[*]
C[&]
D[#]
Attempts:
3 left
💡 Hint
Common Mistakes
Using symbols like '#' or '&' inside brackets which cause errors.
Omitting brackets entirely.
4fill in blank
hard

Fill 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'
Acolors
B[*]
C[@]
Dfruits
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the array and length call.
Using '*' instead of '@' which also works but '@' is preferred.
5fill in blank
hard

Fill 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'
Acelestial
B${#celestial[@]}
C$len
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Forgetting the $ when printing the variable.