| by Arround The Web | No comments

Remove a Specific Element from an Array in Bash

Bash handles only the associative arrays and the one-dimensional numerically indexed arrays. In Bash scripting, arrays do not necessarily consist of a group of associated elements. The array elements can be strings and numbers because Bash does not differentiate between them. In this article, we will perform the deletion operations on the Bash arrays with different approaches. We need to know the index position of the element in the array or key to remove it from the array. The “unset” command is commonly used in Bash to remove an element.

Example 1: Removing a Specific Array Element in the Bash Script

The unset command is utilized to erase the particular element from the array. The unset command requires the name of the variable where the array is defined as well as the element’s index. In the following Bash script, we first create the “MyArray” variable using the “declare” keyword with the “-a” option. Then, the “MyArray” variable is inserted with the string elements. Then, we deploy the “unset” command to delete the array element. The unset command is passed with the array variable name, “MyArray”, as an argument. The “MyArray” is set with the index [2] which deletes the array element that is placed at this index.

Next, we have the echo command which is used to display the updated array after the deletion operation. The “MyArray[@]}”expression is used to print the array where the “@” symbol represents the array index.

#!/bin/bash

declare -a MyArray=( "Panda" "Owl" "Cat" "Rabbit" "Bear" )

unset MyArray[2]

echo "${MyArray[@]}"

As we delete the index value of “2” from the specified array using the unset command, the value that is placed inside the array at index “2” is “Cat” which is removed from the array. And the modified array is displayed in the following output. Note that the array index at all times begins from the number of “0”:

Example 2: Removing a Specific Array Element in the Bash Script Via a Relative Index

The elements are referred from the last element if we use the indices like -1, -2, and so on. Thus, we can also delete or alter them for the previous elements using relative ordering. Let’s have the following Bash script, where the first step of declaring a variable for an array is using the “declare -a” command of the Bash. The array variable is titled “languages”. We insert four different programming languages inside it along with the index number. After that, we have a Bash unset command where the “languages” variable is set with the “-3” relative index to delete the array element. Then, we use the “${languages[@]}” expression to print the newly formed array after using the echo command. The particular element is unset from the array.

#!/bin/usr/env bash

declare -a languages=(
[0]=Java
[1]=Kotlin
[2]=Python
[3]=Scala
)

unset languages[-3]
echo "${languages[@]}"

The output displays the array where the “Kotlin” value is removed because the “-3” index indicates the reverse index direction which is “1” index from the default order. Hence, it is relatively simpler to refer to specific items in a large given array.

Example 3: Removing a Specific Array Element in the Bash Script Using Regex

The recent implementation of the Bash script of removing a specific element from the given array is very simple to accomplish. Now, we have another case where the elements of one array are removed from another array. The following Bash scripts remove any element from the array that matches a regex. We first declare the “arr1” variable which is adjusted with some integer value.

Then, we create another array which is “arr2” and insert two integer values that are also present in the first array, “arr1”. After this, we define a “result” variable that has the expression (“${arr1[@]}”) which shows that the results variable has all the array elements of “arr1” array to print. Next, we set the for-loop which iterates over all the array elements that are present in the “arr2” array and store them in the “ele” object.

Next, we use the “result” variable which is provided with the “(${result[@]/*${ele}*/})” expression. The “result[@]”expression here stores the array elements of the “arr1” and the “${ele}” expression has the array elements of “arr2”. The “arr2” matches those elements from the “arr1” array and deletes them from the “arr1” array. The echo command is then deployed to display the remaining elements of the “arr1” array.

#!/bin/bash

arr1=(11 22 33 44 55 66 77 88 99)
arr2=(22 77)

result=("${arr1[@]}")

for ele in "${arr2[@]}"; do
result=(${result[@]/*${ele}*/})
done

echo "Arr1 new elements are: ${result[@]}"

After matching the regex, the output displays the element where the “22” and “77” elements are removed from the “arr1”.

Example 4: Removing the Entire Array in the Bash Script

The aforementioned Bash scripts are used to remove the particular elements from the array. We can also remove the entire elements of the array by specifying the array variable to the unset command. Here, we implement the deletion operation in the Bash script. The initial step is the declaration of the array which we accomplished with the “declare -a” command.

We define the variable for the array as “StrArray”. Then, we add the string items inside the array. After that, we call the unset command which takes the “strArray” array name without any index value. This approach is used to delete the entire array in the Bash script. In the end, we employ the two echo commands to display the array and the keys of the array.

#!/bin/bash

declare -a StrArray=( "one""two""three""four""five" )

unset StrArray

echo ${!StrArray[@]}

echo ${!StrArray[@]}

We attempt to print the aforementioned script’s elements and keys, which returns no output. Since the array no longer exists, hence a blank result is returned.

Example 5: Removing a Specific Array Element in the Bash Script with Another Approach

The previous examples use the unset command to delete an entire or specific element from a given array. There, we have another technique to delete the particular element of the array without using the unset command. The following Bash script is defined with the array which has the first five characters. That array is stored in a “CharArray” variable.

Then, the “CharArray” variable is called again and is specified with the “${CharArray[@]:1” expression. The expression removes the element of the array which is present at the first occurrence in the array as the index value of “1” is provided to the “CharArray[@]”. After eliminating the specified element from the array, the echo command generates the array which contains all the elements except the removed element.

CharArray=(a b c d e f)

CharArray=("${CharArray[@]:1}")

echo ${CharArray[@]}

The character element of “a” is not shown in the following output array because it is deleted via the “${CharArray[@]:1” expression.

Conclusion

The article about deleting a specific element from an array in a Bash script is explained conceptually. Here, we used the unset command to remove the entire array and the particular element from the array. The unset command also removes the element from the array through the relative indices approach. The match regex is also given to delete the element from the array. Additionally, we included the match regex to remove a certain element from an array.

Share Button

Source: linuxhint.com

Leave a Reply