Bash で文字列の配列をループしますか? 質問する

Bash で文字列の配列をループしますか? 質問する

15 個の文字列 (配列の可能性もある) をループするスクリプトを書きたいのですが、それは可能ですか?

何かのようなもの:

for databaseName in listOfNames
then
  # Do something
end

ベストアンサー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"
                )

おすすめ記事