最後更新: 2024-09-25
目錄
- 定義 Array (Explicit / Indirect)
- Access element in array
- Add Item & Pop Item
- Iterate through an array
- ary[*] 與 ary[@] 的分別
- Length of Array
- Copying an Array
- Aarry Extraction
- Load Content of a File into an Array
- Array as command Args
Bash 的 Array
Bash 支援以下兩種 Array
- One-dimensional indexed(integers) array
- Associative array
注意
-
No maximum limit on the size of an array
No requirement that members be indexed or assigned contiguously - Spaces in Array Elements
定義 Array (Explicit / Indirect)
Explicit declaration of an array
Indexed array
declare -a arrayname
Associative arrays
declare -A arrayname
Indirect indexed array declaration
[1] Arrays are assigned to using compound assignments of the form
arrayname[index]=value
#
arrayname[0]="value0" arrayname[1]="value1" arrayname[2]="value2"
[2] 使用 "()"
arrayname=(value0 value1 value3)
A) 空的 array
ARRAY=()
B) 多行寫法
arrayname=( A B C )
Access element in array
# Any element of an array may be referenced using
$arrayname # 相當於 ${arrayname[0]}
${arrayname} # 相當於 ${arrayname[0]}
${arrayname[index]}
* The braces are required to avoid conflicts with the shell’s filename expansion operators
Add Item & Pop Item
Add Item
array=() # 注意 "=" 兩邊沒有空格
array+=('elementToAdd')
Add an element
Unix=("${Unix[@]}" "AIX" "HP-UX")
Pop Item
array=("${array[@]:1}") # removed the 1st element
array=( "${array[@]/$delete}" ) # removes prefixes matching $delete
unset
unset array # removes the entire array
unset array[i] # destroys the element at index
Iterate through an array
#!/bin/bash # 這樣寫好看點 arrayname=( A B C ) for i in "${array[@]}" do echo "$i" done
ary[*] 與 ary[@] 的分別
# These subscripts differ only when the word appears within double quotes
=> There is no difference if the array expansion is unquoted.
- "${name[*]}"
- "${name[@]}"
當有 quotes 時
- "a[*]" returns a single word that contains all the elements of the array separated by spaces
- "a[@]" returns each element of the array as an individual word (因此可用於 "for loop" !!)
e.g.
LIST=(1 2 3) for i in "${LIST[*]}"; do # echo "$i" # result: "1 2 3" done for i in "${LIST[@]}"; do echo "$i" # result: 一行一個 done
Length of Array
${#array[@]} # The length of arrary
${#array[index]} # The length of ${name[subscript]}
Copying an Array
Color=(Red Yellow)
NewColor=(${Color[@]}) # 必須加上 (), 否則它成了 STRING
NewColor=${Color[@]} # echo ${#NewColor[@]} 的 result 是 1 !!
Concatenation 2 Array
MyColor=(${Color[@]} ${NewColor[@]})
Aarry Extraction
Extraction by offset
${array[@]:offset}
e.g.
ary=(1 2 3 4 5); echo ${ary[@]:2} # 由第 3 個 Item 開始到尾 (index 由 0 開始)
3 4 5
bash get last n elements of array
echo ${arr[@]:(-3)}
echo ${arr[@]: -3} # ":" 與 "-3" 之間的空格必須保留
# The space is required to avoid the interpretation of : followed by a minus - as the expansion of "${var:-abc}" (Use Default Values).
Extraction by offset and length
${array[@]:offset:length}
e.g.
ary=(1 2 3 4 5); echo ${ary[@]::2} # 相當於由 index 0 開始
ary=(1 2 3 4 5); echo ${ary[@]:2:2}
3 4
Load Content of a File into an Array
#!/bin/bash filecontent=( `cat "logfile" `) for t in "${filecontent[@]}" do echo $t done
Array as command Args
rsync_options=( -rnv --exclude='.*' )
rsync "${rsync_options[@]}" source/ target
remark
"..." # 是必需的