一尘不染

循环遍历 Bash 中的字符串数组?

javascript

我想编写一个循环遍历 15 个字符串的脚本(可能是数组?)这可能吗?

就像是:

for databaseName in listOfNames
then
  # Do something
end

阅读 152

收藏
2022-02-14

共1个答案

一尘不染

你可以像这样使用它:

## declare an array variable
declare -a arr=("element1" "element2" "element3")

## now loop through the above array
for i in "${arr[@]}"
do
   echo "$i"
   # or do whatever with individual element of the array
done

# You can access them using echo "${arr[0]}", "${arr[1]}" also

也适用于多行数组声明

declare -a arr=("element1" 
                "element2" "element3"
                "element4"
                )
2022-02-14