Adding elements to shell array and precautions

Time:2021-9-13

There are many ways to add elements to shell arrays. Each method has some points to pay attention to when using it. I haven’t seen any summary materials in this regard, so I’ll summarize it myself.

Direct subscript addition


array_name[index]=value

The simplest way to add is to directly use the subscript index to assign values to array elements. When using, you should pay attention to ensuring that there is no value at the subscript index, otherwise the original value at the index will be replaced.

Array length addition

array_name[${#array_name[@]}]=value
#Or array_ name[${#array_name[*]}]=value

Add with the array length as the subscript, and only one element can be added at a time. However, this method has a premise that the element subscripts in the array must be continuous, otherwise there will be the risk of replacing existing elements. For example:


arr=([1]="a" [2]="b")
echo ${arr[@]}
arr[${#arr[@]}]="c"
echo ${arr[@]}

Instead of adding an element, replace the element value “B” with “C” with subscript 2.

Recreate array


array_name=("${array_name[@]}" value1 ... valueN)

This method does not require continuous subscripts. Multiple elements can be added at the same time, but there are some points to pay attention to:

First, after adding elements in this way, the subscript of the original element in the array will be reset and become continuous from 0. This method cannot be used if you do not want to change the subscript.

Secondly, double quotation marks cannot be omitted, otherwise, when the array array_ When there is an element with spaces in name, the element will be split into multiple by spaces.

Finally, you cannot replace “@” with “*”, although you can replace each other when outputting array elements. If you replace it with “*”, it will behave the same as “@” without double quotation marks. When double quotation marks are added, the array will be_ All elements in name are added to the array as one element. Similar to the difference between the special variables [email protected] and $*.


#!/bin/bash
arr1=()
initArray(){
  arr1=([2]="a b" [3]=2 [5]=4)
}
showArray(){
  echo "Elements in arr1: ${arr1[@]}"
  echo "Length of arr1: ${#arr1[@]}"
  echo "The 3rd elements is: ${arr1[2]}"
  echo
}
initArray
echo "original arr1:"
showArray
echo "add new elements 3 and 5"
echo "--------------------"
echo "use @ without quote"
arr1=(${arr1[@]} 3 5)
showArray
initArray
echo "use * without quote"
arr1=(${arr1[*]} 3 5)
showArray
initArray
echo "use @ with quote"
arr1=("${arr1[@]}" 3 5)
showArray
initArray
echo "use * with quote"
arr1=("${arr1[*]}" 3 5)
showArray

The operation result is:

original arr1:
Elements in arr1: a b 2 4
Length of arr1: 3
The 3rd elements is: a b
 
add new elements 3 and 5
——————–
use @ without quote
Elements in arr1: a b 2 4 3 5
Length of arr1: 6
The 3rd elements is: 2
 
use * without quote
Elements in arr1: a b 2 4 3 5
Length of arr1: 6
The 3rd elements is: 2
 
use @ with quote
Elements in arr1: a b 2 4 3 5
Length of arr1: 5
The 3rd elements is: 4
 
use * with quote
Elements in arr1: a b 2 4 3 5
Length of arr1: 3
The 3rd elements is: 5

Assignment operator+=


array_name+=(value1 ... valueN)

This method does not require continuous element subscripts. Multiple elements can be added. After adding, the element subscripts will not be reset, and there is no risk of element coverage. The only thing to note is that “+ =” cannot be preceded by spaces, and the subsequent elements to be added must be surrounded by “()” and multiple elements must be separated by spaces. The index of the newly added element depends on the index of the last element with value in the original array.


#!/bin/bash
arr1=()
initArray(){
  arr1=([2]="a b" [3]=2 [5]=4)
}
showArray(){
  echo "Elements in arr1: ${arr1[@]}"
  echo "Length of arr1: ${#arr1[@]}"
  echo "The 3rd elements is: ${arr1[2]}"
  echo
}
initArray
echo "original arr1:"
showArray
echo "add new elements 3 and 5"
echo "--------------------"
echo "use += "
arr1+=(3 5)
showArray
echo "The 8th elements is: ${arr1[7]}"

The operation result is:

original arr1:
Elements in arr1: a b 2 4
Length of arr1: 3
The 3rd elements is: a b
 
add new elements 3 and 5
——————–
use +=
Elements in arr1: a b 2 4 3 5
Length of arr1: 5
The 3rd elements is: a b
 
The 8th elements is: 5

The original element in the array has not changed, and the newly added element subscript is incremented based on the subscript 5 of the last valuable element.

summary

Add method grammar

Multiple elements can be added

Subscripts must be continuous Subscript changes after adding The original element may be overwritten
Direct subscript addition array_name[index]=value no no no yes
Array length addition

array_name[${#array_name[@]}]=value

Or array_ name[${#array_name[*]}]=value

no yes no yes
Recreate array array_name=(“${array_name[@]}” value1 … valueN) yes no yes no
Assignment operator+= array_name+=(value1 … valueN) yes yes yes no

This is the end of this article on adding elements and precautions to shell arrays. For more information about adding elements to shell arrays, please search previous articles of developeppaer or continue to browse the following articles. I hope you will support developeppaer in the future!