在Bash中循环遍历字符串数组? [英] Loop through an array of strings in Bash?

查看:122
本文介绍了在Bash中循环遍历字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to write a script that loops through 15 strings (array possibly?) Is that possible?

类似的东西:

for databaseName in listOfNames
then
  # Do something
end

推荐答案

您可以像这样使用它:

## 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"
                )

这篇关于在Bash中循环遍历字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆