在 Bash 中循环遍历字符串数组? [英] Loop through an array of strings in Bash?
本文介绍了在 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屋!
查看全文