0
0
Bash Scriptingscripting~5 mins

Adding and removing elements in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
How do you add an element to an array in Bash?
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
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]
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
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
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)
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.