Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add an element to the end of a bash array.
Bash Scripting
my_array=(apple banana cherry)
my_array+=([1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to quote the element.
Using '=' instead of '+=' which replaces the array.
✗ Incorrect
The '+=' operator adds the new element to the end of the array. Here, "egg" is added.
2fill in blank
mediumComplete the code to remove the second element from a bash array.
Bash Scripting
my_array=(apple banana cherry date)
unset my_array[[1]] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 instead of 1 for the second element index.
Trying to remove element by value instead of index.
✗ Incorrect
Array indexes start at 0, so the second element is at index 1. Using 'unset' removes it.
3fill in blank
hardFix the error in the code to correctly add an element to the array.
Bash Scripting
my_array=(apple banana cherry) my_array=[1] "date"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' which replaces the entire array.
Using non-existent commands like 'append' or 'add'.
✗ Incorrect
The '+=' operator appends elements to the array. '=' replaces the array, and 'append' or 'add' are invalid in bash.
4fill in blank
hardFill both blanks to create an array and then remove the last element.
Bash Scripting
my_array=([1]) unset my_array[[2]]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong indexes to remove elements.
Incorrectly formatting the array elements.
✗ Incorrect
The array is created with three elements indexed 0,1,2. The last element is at index 2, so unset removes it.
5fill in blank
hardFill all three blanks to add two elements and then remove the first element from the array.
Bash Scripting
my_array=(apple) my_array+=([1]) my_array+=([2]) unset my_array[[3]]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Removing the wrong index.
Trying to add multiple elements in one '+=' without quotes.
✗ Incorrect
We add "banana" and "cherry" to the array, then remove the first element at index 0.