Use the syntax: array+=("new_element"). This appends new_element to the end of the array.
Click to reveal answer
beginner
How can you remove an element from a Bash array by index?
Use unset array[index] to remove the element at the specified index from the array.
Click to reveal answer
intermediate
What happens if you unset an array element in Bash?
The element at that index is removed, but the array keeps its other elements and indexes. The array length decreases by one.
Click to reveal answer
intermediate
How do you add multiple elements to a Bash array at once?
Use array+=("elem1" "elem2" "elem3") to add several elements in one command.
Click to reveal answer
advanced
Can you remove an element from a Bash array by value directly?
No, Bash does not have a direct way to remove by value. You need to loop through the array and rebuild it without the unwanted value.
Click to reveal answer
Which command adds an element 'apple' to a Bash array named fruits?
Afruits+=("apple")
Bfruits+="apple"
Cfruits[+]=apple
Dadd fruits apple
✗ Incorrect
The correct syntax to add an element to a Bash array is array+=("element").
How do you remove the element at index 2 from a Bash array named items?
Aunset items[2]
Bremove items 2
Citems-=2
Ddelete items[2]
✗ Incorrect
Use unset items[2] to remove the element at index 2.
What happens to the array indexes after unsetting an element in Bash?
AIndexes shift left automatically
BArray length stays the same
CArray is completely reset
DIndexes remain the same; the element is removed
✗ Incorrect
Unsetting an element removes it but does not shift other elements; indexes remain as they were.
Which of these is a correct way to add multiple elements to a Bash array named colors?
Acolors[+]=red green blue
Bcolors+="red,green,blue"
Ccolors+=("red" "green" "blue")
Dadd colors red green blue
✗ Incorrect
You can add multiple elements by listing them inside parentheses with +=.
Is there a direct Bash command to remove an element by its value from an array?
AYes, use remove_by_value
BNo, you must loop and rebuild the array
CYes, use unset_by_value
DYes, use array.remove(value)
✗ Incorrect
Bash does not support removing by value directly; you must manually filter the array.
Explain how to add and remove elements in a Bash array with examples.
Think about how you add one or many elements and how you remove by index.
You got /4 concepts.
Describe why Bash does not allow removing array elements by value directly and how you can work around it.
Consider how you would filter a list manually.
You got /4 concepts.
Practice
(1/5)
1. Which command correctly adds the element "apple" to the end of a Bash array named fruits?
easy
A. fruits+=("apple")
B. fruits[0]+="apple"
C. fruits.append("apple")
D. fruits.add("apple")
Solution
Step 1: Understand Bash array addition syntax
In Bash, to add an element to an array, use the syntax array+=("element").
Step 2: Check each option
fruits+=("apple") uses the correct syntax. Options A, B, and C are invalid Bash syntax for arrays.
Final Answer:
fruits+=("apple") -> Option A
Quick Check:
Add element to array = fruits+=("apple") [OK]
Hint: Use += with parentheses to add elements to arrays [OK]
Common Mistakes:
Using string concatenation instead of array syntax
Using methods like append() which are not Bash
Forgetting parentheses around the element
2. Which of the following is the correct syntax to remove the element at index 2 from a Bash array named colors?
easy
A. unset colors[2]
B. remove colors[2]
C. delete colors[2]
D. colors-=2
Solution
Step 1: Recall how to remove array elements in Bash
Bash uses the unset command with the array element's index to remove it.
Step 2: Verify each option
unset colors[2] uses unset colors[2], which is correct. Options A, C, and D are not valid Bash commands for removing array elements.
Final Answer:
unset colors[2] -> Option A
Quick Check:
Remove element by index = unset colors[2] [OK]
Hint: Use unset with array[index] to remove elements [OK]
Common Mistakes:
Using non-existent commands like remove or delete
Trying to subtract elements with -= which is invalid
Forgetting to specify the index inside brackets
3. What is the output of this Bash script?
items=("pen" "pencil" "eraser")
items+=("marker")
unset 'items[1]'
for item in "${items[@]}"; do
echo "$item"
done
medium
A. pen
marker
B. pen
pencil
eraser
marker
C. pen
eraser
marker
D. pen
pencil
marker
Solution
Step 1: Analyze array modifications
Initial array: ["pen", "pencil", "eraser"]. Then add "marker" at the end: ["pen", "pencil", "eraser", "marker"]. Then remove element at index 1 ("pencil").
Step 2: Determine final array elements
After removal, array elements are: index 0="pen", index 2="eraser", index 3="marker". Looping over ${items[@]} skips unset indices, so output is "pen", "eraser", "marker" each on a new line.
Final Answer:
pen
eraser
marker -> Option C
Quick Check:
Remove index 1 and add marker = pen, eraser, marker [OK]
Hint: Unset removes element but keeps others; loop skips unset [OK]
Common Mistakes:
Assuming unset shifts elements left
Expecting pencil to still print
Not realizing loop skips unset indices
4. Identify the error in this Bash script that tries to remove the first element from array nums:
nums=(1 2 3 4)
unset nums(0)
echo "${nums[@]}"
medium
A. Array elements must be strings, not numbers
B. Missing quotes around array name in unset
C. Cannot unset elements in Bash arrays
D. Using parentheses instead of brackets in unset
Solution
Step 1: Check unset syntax for array elements
To remove an element, use unset 'array[index]' with square brackets, not parentheses.
Step 2: Identify the error in the script
The script uses unset nums(0), which is invalid syntax. It should be unset 'nums[0]'.
Final Answer:
Using parentheses instead of brackets in unset -> Option D
Quick Check:
Unset uses brackets, not parentheses [OK]
Hint: Use square brackets [] for array indices in unset [OK]
Common Mistakes:
Using parentheses () instead of brackets []
Forgetting to quote the unset argument
Thinking unset can't remove array elements
5. You have a Bash array tasks=("clean" "cook" "shop" "study"). You want to remove the task "cook" and add "exercise" at the end. Which script snippet correctly does this?
hard
A. tasks.remove("cook")
tasks.append("exercise")
B. unset 'tasks[1]'
tasks+=("exercise")
C. tasks=("clean" "shop" "exercise" "study")
D. unset tasks[2]
tasks+="exercise"
Solution
Step 1: Remove element "cook" at index 1
Use unset 'tasks[1]' to remove the second element safely.
Step 2: Add "exercise" to the end of the array
Use tasks+=("exercise") to append the new task.
Final Answer:
unset 'tasks[1]'
tasks+=("exercise") -> Option B
Quick Check:
Remove by unset and add with += [OK]
Hint: Unset by index, add with += and parentheses [OK]
Common Mistakes:
Reassigning whole array losing other elements
Using non-Bash methods like remove() or append()
Concatenating string instead of array element
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.