Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print the first element 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 index 1 to get the first element.
Forgetting the square brackets.
Using parentheses instead of brackets.
✗ Incorrect
In bash arrays, the first element is accessed with index 0, so ${arr[0]} prints 'apple'.
2fill in blank
mediumComplete the code to print the last element of the array.
Bash Scripting
arr=(dog cat mouse)
echo ${arr[[1]]} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a negative index.
Using index 3 which is out of range.
Using index 1 which is the second element.
✗ Incorrect
The last element in a 3-element array is at index 2, so ${arr[2]} prints 'mouse'.
3fill in blank
hardFix the error in the code to print the second element of the array.
Bash Scripting
arr=(red green blue)
echo ${arr[[1]]} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or curly braces instead of square brackets.
Using index 2 which is the third element.
Forgetting the dollar sign before the array.
✗ Incorrect
Bash arrays use square brackets for indexing, so ${arr[1]} prints 'green'.
4fill in blank
hardFill the three blanks to print the third element of the array.
Bash Scripting
arr=(sun moon stars)
echo ${arr[{BLANK_2}}] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong brackets or missing one bracket.
Using index 3 which is out of range.
Putting brackets in the wrong order.
✗ Incorrect
5fill in blank
hardComplete the code to print the second element of the array.
Bash Scripting
arr=(one two three)
echo ${arr[{BLANK_2}}] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 2 which is the third element.
Missing one of the brackets.
Putting brackets after the index.
✗ Incorrect
The brackets must surround the index.