向Shell數組添加元素有多種方法,每種方法在使用時都有一些需要注意的地方,沒有見過這方面的總結資料,所以自己總結一下。
直接下標添加
最簡單的添加方式,直接用下標index為數組元素賦值,在使用時需要注意的是就是需要確保下標index處原本是沒有值的,否則會替換index處原本的值。
數組長度添加
array_name[${#array_name[@]}]=value
#或array_name[${#array_name[*]}]=value
以數組長度為下標添加,每次只能增加一個元素。但此方法有一個前提,就是數組中的元素下標必須是連續的,否則會存在替換已有元素的風險。例如:
arr=([1]="a" [2]="b")
echo ${arr[@]}
arr[${#arr[@]}]="c"
echo ${arr[@]}
實際上并有添加元素,而是將下標為2的元素值“b”替換為了“c”。
重新創建數組
array_name=("${array_name[@]}" value1 ... valueN)
這種方式不需要下標連續,可同時添加多個元素,但是有一些要注意的地方:
首先,使用此方式添加元素后,數組中原有元素的下標會重置,會從0開始變成連續的,如果不希望改變下標則不能使用這種方式。
其次,雙引號不能省略,否則,當數組array_name中存在包含空格的元素時會按空格將元素拆分成多個。
最后,不能將“@”替換為“*”,雖然在輸出數組元素時可以相互替換,如果替換為“*”,不加雙引號時與“@”的表現一致,加雙引號時,會將數組array_name中的所有元素作為一個元素添加到數組中。類似于特殊變量$@和$*的區別。
#!/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
運行結果為:
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
賦值運算符+=
array_name+=(value1 ... valueN)
這種方式不需要元素下標連續,可以添加多個元素,添加后元素下標不會重置,不存在元素覆蓋的風險。唯一要注意的就是“+=”前后不能有空格,并且后面的待添加元素必須用“()”包圍起來,并且多個元素用空格分隔。新添加的元素的下標取決于原本數組中最后有值的元素的下標。
#!/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]}"
運行結果為:
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
數組中原本的元素沒有改變,新添加的元素下標是在最后有值的元素下標5的基礎上遞增的。
總結
添加方式 |
語法 |
可添加多個元素
|
下標必須連續 |
添加后下標改變 |
可能覆蓋原有元素 |
直接下標添加 |
array_name[index]=value |
否 |
否 |
否 |
是 |
數組長度添加 |
array_name[${#array_name[@]}]=value
或array_name[${#array_name[*]}]=value
|
否 |
是 |
否 |
是 |
重新創建數組 |
array_name=("${array_name[@]}" value1 ... valueN) |
是 |
否 |
是 |
否 |
賦值運算符+= |
array_name+=(value1 ... valueN) |
是 |
是 |
是 |
否 |
到此這篇關于Shell數組添加元素及注意事項的文章就介紹到這了,更多相關Shell數組添加元素內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- PowerShell中使用ArrayList實現數組插入、刪除、添加例子
- Shell腳本數組操作小結
- Shell腳本數組用法小結
- shell for循環與數組應用介紹
- shell 使用數組作為函數參數的方法(詳解)
- shell腳本字符串和數組的使用操作方法
- Linux Shell 數組的創建及使用技巧
- shell數組操作簡明總結