## declare an array variabledeclare -a arr=("element1" "element2" "element3")
## now loop through the above arrayfor i in "${arr[@]}"doecho "$i"# or do whatever with individual element of the arraydone
# You can access them using echo "${arr[0]}", "${arr[1]}" also
也适用于多行数组声明
declare -a arr=("element1""element2" "element3""element4")
listOfNames="RARBR CRD"
# To allow for other whitespace in the string:# 1. add double quotes around the list variable, or# 2. see the IFS note (under 'Side Notes')
for databaseName in "$listOfNames" # <-- Note: Added "" quotes.doecho "$databaseName" # (i.e. do action / processing of $databaseName here...)done
# Outputs# RA# RB# R C# RD
B.名称中没有空格:
listOfNames="RARBR CRD"
for databaseName in $listOfNames # Note: No quotesdoecho "$databaseName" # (i.e. do action / processing of $databaseName here...)done
# Outputs# RA# RB# R# C# RD
# line delimited (each databaseName is stored on a line)while read databaseNamedoecho "$databaseName" # i.e. do action / processing of $databaseName here...done # <<< or_another_input_method_here
#!/bin/bash# declare an array called files, that contains 3 valuesfiles=( "/etc/passwd" "/etc/group" "/etc/hosts" )for i in "${files[@]}"doecho "$i"done
#!/bin/bash## declare an array variabledeclare -a array=("one" "two" "three")
# get length of an arrayarraylength=${#array[@]}
# use for loop to read all values and indexesfor (( i=0; i<${arraylength}; i++ ));doecho "index: $i, value: ${array[$i]}"done
输出:
index: 0, value: oneindex: 1, value: twoindex: 2, value: three
set -A databaseName=("db1" "db2" ....) ||declare -a databaseName=("db1" "db2" ....)# now loopfor dbname in "${arr[@]}"doecho "$dbname" # or whatever
done
for Item in Item1 Item2 Item3 Item4 ;doecho $Itemdone
输出:
Item1Item2Item3Item4
保留空格;单引号或双引号列表条目和双引号列表扩展。
for Item in 'Item 1' 'Item 2' 'Item 3' 'Item 4' ;doecho "$Item"done
输出:
Item 1Item 2Item 3Item 4
使列表在多行
for Item in Item1 \Item2 \Item3 \Item4doecho $Itemdone
输出:
Item1Item2Item3Item4
简单列表变量
List=( Item1 Item2 Item3 )
或
List=(Item1Item2Item3)
显示列表变量:
echo ${List[*]}
输出:
Item1 Item2 Item3
遍历列表:
for Item in ${List[*]}doecho $Itemdone
输出:
Item1Item2Item3
创建一个函数来遍历列表:
Loop(){for item in ${*} ;doecho ${item}done}Loop ${List[*]}
使用声明关键字(命令)创建列表,技术上称为数组:
declare -a List=("element 1""element 2""element 3")for entry in "${List[@]}"doecho "$entry"done
输出:
element 1element 2element 3
创建关联数组。字典:
declare -A continent
continent[Vietnam]=Asiacontinent[France]=Europecontinent[Argentina]=America
for item in "${!continent[@]}";doprintf "$item is in ${continent[$item]} \n"done
输出:
Argentina is in AmericaVietnam is in AsiaFrance is in Europe
arr=("sharlock" "bomkesh" "feluda" ) ##declare array
len=${#arr[*]} # it returns the array length
#iterate with while loopi=0while [ $i -lt $len ]doecho ${arr[$i]}i=$((i+1))done
#iterate with for loopfor i in $arrdoecho $idone
#iterate with spliceecho ${arr[@]:0:3}
## declare an array variablearr_variable=("kofi" "kwame" "Ama")
## now loop through the above arrayfor i in "${arr_variable[@]}"doecho "$i"
done
您可以使用带有三个表达式(C风格)的计数器遍历bash数组值,以读取循环语法的所有值和索引:
declare -a kofi=("kofi" "kwame" "Ama")
# get the length of the arraylength=${#kofi[@]}
for (( j=0; j<${length}; j++ ));doprint (f "Current index %d with value %s\n" $j "${kofi[$j]}")done