数组的声明:

1. 1)array[key]=value # array[0]=one,array[1]=two

1. 2)declare -a array # array被当作数组名

1. 3)array=( value1 value2 value3 ... )

1. 4)array=( [1]=one [2]=two [3]=three ... )

1. 5)array="one two three" # echo \${array[0|@|*]},把array变量当作数组来处理，但数组元素只有字符串本身

1. 1)\${array[key]} # \${array[1]}

1. 1)unset array[1] # 删除数组中第一个元素

1. 2)unset array # 删除整个数组

1. 1)\${#array}

1. 2)\${#array[0]} #同上。 \${#array[*]} 、\${#array[@]}。注意同#{array:0}的区别

array=( [0]=one [1]=two [2]=three [3]=four )
\${array[@]:1} # two three four,除掉第一个元素后所有元素，那么\${array[@]:0}表示所有元素
\${array[@]:0:2} # one two
\${array[@]:1:2} # two three

1. [root@localhost dev]# echo \${array[@]:0}
2. one two three four

1. [root@localhost dev]# echo \${array[@]#t*e} # 左边开始最短的匹配:"t*e",这将匹配到"thre"
2. one two e four

1. [root@localhost dev]# echo \${array[@]##t*e} # 左边开始最长的匹配,这将匹配到"three"

1. [root@localhost dev]# array=( [0]=one [1]=two [2]=three [3]=four )

1. [root@localhost dev]# echo \${array[@] %o} # 从字符串的结尾开始最短的匹配
2. one tw three four

1. [root@localhost dev]# echo \${array[@] %%o} # 从字符串的结尾开始最长的匹配
2. one tw three four

1. [root@localhost dev]# array=( [0]=one [1]=two [2]=three [3]=four )

1. [root@localhost dev]# echo \${array[@] /o/m}
2. mne twm three fmur

1. [root@localhost dev]# echo \${array[@] //o/m}
2. mne twm three fmur

1. [root@localhost dev]# echo \${array[@] //o/}
2. ne tw three fur

1. [root@localhost dev]# echo \${array[@] /#o/k}
2. kne two three four

1. [root@localhost dev]# echo \${array[@] /%o/k}
2. one twk three four

push:

```array=(”\${array[@]}” \$new_element)

pop:
array=(\${array[@]:0:\$((\${#array[@]}-1))})

shift:
array=(\${array[@]:1})

unshift
array=(\$new_element “\${array[@]}”)

function del_array {
local i
for (( i = 0 ; i < \${#array[@]} ; i++ ))
do
if [ "\$1" = "\${array[\$i]}” ] ;then
break
fi
done
del_array_index \$i
}

function del_array_index {
array=(\${array[@]:0:\$1} \${array[@]:\$((\$1 + 1))})
}```