数组是包含多个相同或不同数据类型的集合,数组索引从零开始。本文有 15 个用 bash 操作数组的例子。
1. 声明数组,赋值
当有变量为如下格式的时候,Bash 会自动创建数组。
- name 为数组名称
- index 为任意数字,或表达式的最终计算值大于等于零。可以显式声明数组通过 declare -a arrayname
$ cat arraymanip.sh
#! /bin/bash
Unix[0]='Debian'
Unix[1]='Red hat'
Unix[2]='Ubuntu'
Unix[3]='Suse'
echo ${Unix[1]}
$./arraymanip.sh
Red hat
访问数组元素使用括号,如 ${name[index]}
2. 声明时初始化数组
通过指定元素列表来声明数组,就不用一个一个分别初始化数组元素的,用空格隔开在括号里。
Syntax:
declare -a arrayname=(element1 element2 element3)
如果数组元素包含空格符,用引号括起来。
#! /bin/bash
$cat arraymanip.sh
declare -a Unix=('Debian' 'Red hat' 'Red hat' 'Suse' 'Fedora');
declare -a 声明一个数组以及括号中的所有元素为数组元素。
3. 输出整个 Bash 数组
有多种不同的方法来输出整个数组。如果索引是 @ 或者 *,则引用数组所有元素。也可以使用循环,遍历数组中每个元素再输出。
echo ${Unix[@]}
# Add the above echo statement into the arraymanip.sh
#./t.sh
Debian Red hat Ubuntu Suse
如果引用数组元素,而不提供索引的话,就是引用数组的第一个元素,即索引为零的元素。
4. Bash 数组长度
可以使用特殊参数 $# 来获得数组长度。
${#arrayname[@]} gives you the length of the array.
$ cat arraymanip.sh
declare -a Unix=('Debian' 'Red hat' 'Suse' 'Fedora');
echo ${#Unix[@]} #Number of elements in the array
echo ${#Unix} #Number of characters in the first element of the array.i.e Debian
$./arraymanip.sh
4
6
5. 数组第 n 个元素的长度
${#arrayname[n]} 为数组第 n 个元素的长度。
$cat arraymanip.sh
#! /bin/bash
Unix[0]='Debian'
Unix[1]='Red hat'
Unix[2]='Ubuntu'
Unix[3]='Suse'
echo ${#Unix[3]} # length of the element located at index 3 i.e Suse
$./arraymanip.sh
4
6. 指定偏移和长度输出数组
下面的例子是输出 2 个数组元素,从第索引为3的元素开始。
$cat arraymanip.sh
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
echo ${Unix[@]:3:2}
$./arraymanip.sh
Suse Fedora
上面的例子返回里索引为 3 和 4 的元素。索引永远由零开始。
7. 根据偏移和长度,输出数组指定元素的一部分
输出一个数组元素的前四个字符。如例,Ubuntu 是数组索引为 3 的元素,可以指定偏移和长度来获取数组指定元素的一部分。
$cat arraymanip.sh
#! /bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
echo ${Unix[2]:0:4}
./arraymanip.sh
Ubun
上面的例子取出索引为 2 的数组元素的前 4 个字符。
8. 搜索和替换数组元素
下面的例子,在数组元素中搜索 Ubuntu,替换为 SCO Unix
$cat arraymanip.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
echo ${Unix[@]/Ubuntu/SCO Unix}
$./arraymanip.sh
Debian Red hat SCO Unix Suse Fedora UTS OpenLinux
但是,这个例子并没有永久替换数组内容。
9. 添加元素到已存在的 Bash Array
下面的例子展示了如何添加元素到已存在数组。
$cat arraymanip.sh
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Unix=("${Unix[@]}" "AIX" "HP-UX")
echo ${Unix[7]}
$./arraymanip.sh
AIX
‘AIX’ and ‘HP-UX’ are added in 7th and 8th index respectively.
在数组 Unix 中,元素 ‘AIX’ 和 ‘HP-UX’ 添加到第 7 和 第 8 位。
10. 删除数组元素
unset 用于移除数组元素,unset 与给数组元素赋值为 null 是一样的效果。
$cat arraymanip.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
unset Unix[3]
echo ${Unix[3]}
以上例子,会输出 null 在索引为 3 的值。下面的例子显示如果完全从数组中删除。
$ cat arraymanip.sh
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
pos=3
Unix=(${Unix[@]:0:$pos} ${Unix[@]:$(($pos + 1))})
echo ${Unix[@]}
$./arraymanip.sh
Debian Red hat Ubuntu Fedora UTS OpenLinux
本例中,${Unix[@]:0:$pos} 将输出 3 个元素从索引为 0 开始。合并以上的输出。这是删除数组元素的一个方法。
11. 使用模式 (Patterns) 删除数组元素
在搜索条件,可以给出模式 (Patterns),存储其余元素到另外一个数组。
$ cat arraymanip.sh
#!/bin/bash
declare -a Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora');
declare -a patter=( ${Unix[@]/Red*/} )
echo ${patter[@]}
$ ./arraymanip.sh
Debian Ubuntu Suse Fedora
以上例子删除数组元素形如 Red*
12. 复制数组
扩展数组元素,存储到新的数组中。
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Linux=("${Unix[@]}")
echo ${Linux[@]}
$ ./arraymanip.sh
Debian Red hat Ubuntu Fedora UTS OpenLinux
13. 连接两个 Bash 数组
扩展两个数组,赋值给新数组。
$cat arraymanip.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh');
UnixShell=("${Unix[@]}" "${Shell[@]}")
echo ${UnixShell[@]}
echo ${#UnixShell[@]}
$ ./arraymanip.sh
Debian Red hat Ubuntu Suse Fedora UTS OpenLinux bash csh jsh rsh ksh rc tcsh
14
输出数组 ‘Unix’ 和 ‘Shell’ 中的所有元素,新数组一共有 14 个元素。
14. 删除整个数组
使用 unset 删除整个数组。
$cat arraymanip.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh');
UnixShell=("${Unix[@]}" "${Shell[@]}")
unset UnixShell
echo ${#UnixShell[@]}
$ ./arraymanip.sh
0
unset 数组后,数组长度为零。
15. 加载文件内容到数组
可以逐行添加文件内容到数组。
#Example file
$ cat logfile
Welcome
to
thegeekstuff
Linux
Unix
$ cat loadcontent.sh
#!/bin/bash
filecontent=( `cat "logfile" `)
for t in "${filecontent[@]}"
do
echo $t
done
echo "Read file content!"
$ ./loadcontent.sh
Welcome
to
thegeekstuff
Linux
Unix
Read file content!
以上例子中,数组中的所有元素均利用循环输出。
http://blog.maxiang.net/15-bash-array-tutorial/148/