Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

arrays - 在Bash中循环遍历字符串数组?(Loop through an array of strings in Bash?)

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

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

Something like:

(就像是:)

for databaseName in listOfNames
then
  # Do something
end
  ask by Mo. translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use it like this:

(您可以像这样使用它:)

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

Also works for multi-line array declaration

(也适用于多行数组声明)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...