數(shù)組是一個包含多個值的變量,它們可以是相同類型或不同類型。沒有最大的數(shù)組大小限制,也沒有要求成員變量被連續(xù)索引或連續(xù)分配。數(shù)組索引從零開始。
本文展示最常用的15個array示例
1.聲明一個數(shù)組并賦值
在bash中,使用以下格式的變量時會自動創(chuàng)建數(shù)組,
name[index]=value
- name 是數(shù)組的名稱
- index 可以是任何數(shù)字或表達式,其值必須等于或大于零。您可以使用 declare -a arrayname聲明一個顯式數(shù)組。
$ 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
要從數(shù)組訪問元素,請使用大括號,例如 ${name[index]}。
2. 在聲明期間初始化數(shù)組
無需單獨初始化數(shù)組的每個元素,而是可以通過使用大括號指定元素列表(由空格分隔)來聲明和初始化數(shù)組。
Syntax:
declare -a arrayname=(element1 element2 element3)
如果元素具有空格字符,請用引號將其引起來。
$cat arraymanip.sh
#! /bin/bash
declare -a Unix=('Debian' 'Red hat' 'Red hat' 'Suse' 'Fedora');
declare -a 聲明一個數(shù)組,括號中的所有元素都是數(shù)組的元素。
3.打印整個Bash陣列
有多種方法可以打印數(shù)組的整個元素。如果索引號是@或*,則引用數(shù)組的所有成員。您可以使用bash中的循環(huán)語句遍歷數(shù)組元素并進行打印。
echo ${Unix[@]}
# 將上面一行添加到 arraymanip.sh
#./arraymanip.sh
Debian Red hat Ubuntu Suse
4. Bash數(shù)組的長度
使用稱為$# 的特殊參數(shù)來獲取數(shù)組的長度。
${#arrayname[@]} 將給出數(shù)組長度
$ 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. 數(shù)組中第n個元素的長度
${#arrayname[n]} 給出數(shù)組中第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.按數(shù)組的偏移量和長度提取
以下示例顯示了從名為Unix的數(shù)組中從位置3開始提取2個元素的方法。
$cat arraymanip.sh
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
echo ${Unix[@]:3:2}
$./arraymanip.sh
Suse Fedora
上面的示例返回第三個索引和第四個索引中的元素。索引始終以零開頭。
7. 對于數(shù)組的特定元素,使用偏移量和長度提取
從數(shù)組元素中只提取前四個元素。例如,位于數(shù)組第二個索引的Ubuntu,可以為數(shù)組的特定元素使用offset和length。
$cat arraymanip.sh
#! /bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
echo ${Unix[2]:0:4}
./arraymanip.sh
Ubun
上面的示例從數(shù)組的第二個索引元素中提取前四個字符。
8. 搜索和替換數(shù)組元素
以下示例在數(shù)組元素中搜索 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
在此示例中,它將第二個索引Ubuntu中的元素替換為SCO Unix。但是此示例不會永久替換數(shù)組內(nèi)容。
9. 將元素添加到現(xiàn)有的Bash數(shù)組
$cat arraymanip.sh
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Unix=("${Unix[@]}" "AIX" "HP-UX")
echo ${Unix[7]}
$./arraymanip.sh
AIX
在名為Unix的數(shù)組中,元素AIX和HP-UX 分別添加在第7個索引和第8個索引中。
10. 從數(shù)組中刪除元素
$cat arraymanip.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
unset Unix[3]
echo ${Unix[3]}
上面的腳本將只打印null,這是第3個索引中可用的值。以下示例顯示了從數(shù)組中完全刪除元素的一種方法。
$ 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} 從 Unix 中拿出前3個元素, ${Unix[@]:4} 從Unix中拿出后四個元素. 把他們合并在一起成一個新的數(shù)組. 這也是種刪除數(shù)組元素的方法
11. 匹配刪除數(shù)組元素
在搜索條件中,您可以給出匹配值,并將剩余的元素存儲到另一個數(shù)組中,如下所示。
$ 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. 復制數(shù)組
展開數(shù)組元素,然后將其存儲到新數(shù)組中,如下所示。
#!/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數(shù)組的串聯(lián)
展開兩個數(shù)組的元素,然后將其分配給新數(shù)組。
$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
14 刪除整個數(shù)組
unset arrayName
$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
15. 將文件內(nèi)容加載到數(shù)組中
#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!
本文翻譯自 https://www.thegeekstuff.com/2010/06/bash-array-tutorial/