回答:
次のように使用できます。
## 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"
                )もちろん、それは可能です。
for databaseName in a b c d e f; do
  # do something like: echo $databaseName
done 詳細については、bashループを参照してください。
for year in $(seq 2000 2013)ます。
                    DATABASES="a b c d e f"。
                    これらの回答にはカウンターは含まれていません...
#!/bin/bash
## declare an array variable
declare -a array=("one" "two" "three")
# get length of an array
arraylength=${#array[@]}
# use for loop to read all values and indexes
for (( i=1; i<${arraylength}+1; i++ ));
do
  echo $i " / " ${arraylength} " : " ${array[$i-1]}
done出力:
1  /  3  :  one
2  /  3  :  two
3  /  3  :  threeecho "$i / ${arraylength} : ${array[$i-1]}"-それ以外の場合、$iグロブが含まれている場合は展開され、タブが含まれている場合は変更されますスペースなど
                    はい
for Item in Item1 Item2 Item3 Item4 ;
  do
    echo $Item
  done出力:
Item1
Item2
Item3
Item4スペースを確保するため。一重引用符または二重引用符リストのエントリと二重引用符リストの展開。
for Item in 'Item 1' 'Item 2' 'Item 3' 'Item 4' ;
  do
    echo "$Item"
  done出力:
Item 1
Item 2
Item 3
Item 4複数行にわたってリストを作成するには
for Item in Item1 \
            Item2 \
            Item3 \
            Item4
  do
    echo $Item
  done出力:
Item1
Item2
Item3
Item4
単純なリスト変数
List=( Item1 Item2 Item3 )または
List=(
      Item1 
      Item2 
      Item3
     )リスト変数を表示します。
echo ${List[*]}出力:
Item1 Item2 Item3リストをループします。
for Item in ${List[*]} 
  do
    echo $Item 
  done出力:
Item1
Item2
Item3リストを処理する関数を作成します。
Loop(){
  for item in ${*} ; 
    do 
      echo ${item} 
    done
}
Loop ${List[*]}declareキーワード(コマンド)を使用して、技術的に配列と呼ばれるリストを作成します。
declare -a List=(
                 "element 1" 
                 "element 2" 
                 "element 3"
                )
for entry in "${List[@]}"
   do
     echo "$entry"
   done出力:
element 1
element 2
element 3連想配列を作成します。辞書:
declare -A continent
continent[Vietnam]=Asia
continent[France]=Europe
continent[Argentina]=America
for item in "${!continent[@]}"; 
  do
    printf "$item is in ${continent[$item]} \n"
  done出力:
 Argentina is in America
 Vietnam is in Asia
 France is in EuropeリストへのCVS変数またはファイル。
内部フィールド区切り文字をスペースから、必要なものに変更します。
以下の例では、コンマに変更されています
List="Item 1,Item 2,Item 3"
Backup_of_internal_field_separator=$IFS
IFS=,
for item in $List; 
  do
    echo $item
  done
IFS=$Backup_of_internal_field_separator出力:
Item 1
Item 2
Item 3それらに番号を付ける必要がある場合:
` これはバックティックと呼ばれます。コマンドをバックティックの中に入れます。
`commend` キーボードの1番の横、またはタブキーの上にあります。標準的なアメリカ英語のキーボード。
List=()
Start_count=0
Step_count=0.1
Stop_count=1
for Item in `seq $Start_count $Step_count $Stop_count`
    do 
       List+=(Item_$Item)
    done
for Item in ${List[*]}
    do 
        echo $Item
    done出力は次のとおりです。
Item_0.0
Item_0.1
Item_0.2
Item_0.3
Item_0.4
Item_0.5
Item_0.6
Item_0.7
Item_0.8
Item_0.9
Item_1.0バスシの振る舞いに慣れる:
ファイルにリストを作成する
cat <<EOF> List_entries.txt
Item1
Item 2 
'Item 3'
"Item 4"
Item 7 : *
"Item 6 : * "
"Item 6 : *"
Item 8 : $PWD
'Item 8 : $PWD'
"Item 9 : $PWD"
EOFリストファイルをリストに読み込んで表示する
List=$(cat List_entries.txt)
echo $List
echo '$List'
echo "$List"
echo ${List[*]}
echo '${List[*]}'
echo "${List[*]}"
echo ${List[@]}
echo '${List[@]}'
echo "${List[@]}""${List[@]}"、正しいことが引用符で。${List[@]}間違っている。${List[*]}間違っている。試してくださいList=( "* first item *" "* second item *" )-の正しい動作が得られますfor item in "${List[@]}"; do echo "$item"; doneが、他のバリアントからは得られません。
                    List=( "first item" "second item" )に分割されますfirst、item、second、item同様)。
                    4ndrewの答えと同じ精神で:
listOfNames="RA
RB
R C
RD"
# To allow for other whitespace in the string:
# 1. add double quotes around the list variable, or
# 2. see the IFS note (under 'Side Notes')
for databaseName in "$listOfNames"   #  <-- Note: Added "" quotes.
do
  echo "$databaseName"  # (i.e. do action / processing of $databaseName here...)
done
# Outputs
# RA
# RB
# R C
# RDB.名前に空白がない:
listOfNames="RA
RB
R C
RD"
for databaseName in $listOfNames  # Note: No quotes
do
  echo "$databaseName"  # (i.e. do action / processing of $databaseName here...)
done
# Outputs
# RA
# RB
# R
# C
# RDノート
listOfNames="RA RB R C RD"出力は同じです。データを取り込む他の方法は次のとおりです。
stdinから読み取る
# line delimited (each databaseName is stored on a line)
while read databaseName
do
  echo "$databaseName"  # i.e. do action / processing of $databaseName here...
done # <<< or_another_input_method_hereIFS='\n'、またはMacOSの場合)IFS='\r')#!/bin/bashスクリプトファイルの先頭に含めると、実行環境を示します。その他のソース(ループの読み取り中)
IFS。(誰にとっても、IFS特定の区切り文字を指定することができます。これにより、他の空白を部分文字列に分離することなく文字列に含めることができます)。
                    $databaseNameリスト全体が含まれるだけなので、1回の反復のみが行われます。
                    まだ誰もこれを投稿していないことに驚いています-配列をループしているときに要素のインデックスが必要な場合は、これを行うことができます:
arr=(foo bar baz)
for i in ${!arr[@]}
do
    echo $i "${arr[i]}"
done出力:
0 foo
1 bar
2 bazこれは、「従来の」forループスタイル(for (( i=0; i<${#arr[@]}; i++ )))。
(${!arr[@]}そして$i、それらは単なる数字であるため、引用する必要はありません。とにかく引用することを提案する人もいますが、それは単なる個人的な好みです。)
これも読みやすいです:
FilePath=(
    "/tmp/path1/"    #FilePath[0]
    "/tmp/path2/"    #FilePath[1]
)
#Loop
for Path in "${FilePath[@]}"
do
    echo "$Path"
doneIFS=$'\n'  これは、このシナリオの他のソリューションでも機能する場合があります。
                    に加えて anubhavaの正解に:ループの基本構文が次の場合:
for var in "${arr[@]}" ;do ...$var... ;doneあります 特殊なケースでは、バッシュ:
スクリプトまたは関数を実行すると、コマンドラインで渡された引数が$@配列変数に割り当てられます$1。$2、$3、など。
これは、(テスト用に)
set -- arg1 arg2 arg3 ...ループの上に、この配列は、単純に書くことができます。
for item ;do
    echo "This is item: $item."
  done注その予約された作品inは存在せず、配列名もない!
サンプル:
set -- arg1 arg2 arg3 ...
for item ;do
    echo "This is item: $item."
  done
This is item: arg1.
This is item: arg2.
This is item: arg3.
This is item: ....これは同じです
for item in "$@";do
    echo "This is item: $item."
  done#!/bin/bash
for item ;do
    printf "Doing something with '%s'.\n" "$item"
  doneスクリプトでこれを保存しmyscript.sh、chmod +x myscript.shその後、
./myscript.sh arg1 arg2 arg3 ...
Doing something with 'arg1'.
Doing something with 'arg2'.
Doing something with 'arg3'.
Doing something with '...'.myfunc() { for item;do cat <<<"Working about '$item'."; done ; }その後
myfunc item1 tiem2 time3
Working about 'item1'.
Working about 'tiem2'.
Working about 'time3'.宣言配列はKornシェルでは機能しません。Kornシェルについては、以下の例を使用してください。
promote_sla_chk_lst="cdi xlob"
set -A promote_arry $promote_sla_chk_lst
for i in ${promote_arry[*]};
    do
            echo $i
    donefor i in ${foo[*]}基本的に常に間違っている- for i in "${foo[@]}"元のリストの境界を保持し、グロブの展開を防ぐフォームです。そして、エコーはする必要がありますecho "$i"
                    Kornシェルを使用している場合は、「set -A databaseName」があり、それ以外の場合は「declare -a databaseNameがあります。」があり。
すべてのシェルで機能するスクリプトを作成するには、
 set -A databaseName=("db1" "db2" ....) ||
        declare -a databaseName=("db1" "db2" ....)
# now loop 
for dbname in "${arr[@]}"
do
   echo "$dbname"  # or whatever
doneすべてのシェルで動作するはずです。
$ bash --versionGNU bash、バージョン4.3.33(0)-release(amd64-portbld-freebsd10.0)       $ set -A databaseName=("db1" "db2" ....) || declare -a databaseName=("db1" "db2" ....)bash:予期しないトークン `( 'の近くの構文エラー
                    これを試して。動作し、テストされています。
for k in "${array[@]}"
do
    echo $k
done
# For accessing with the echo command: echo ${array[0]}, ${array[1]}array=( "hello world" )またはを試してくださいarrray=( "*" )。最初のケースでは印刷されhello、world個別に印刷されます。2番目のケースでは、代わりにファイルのリストが印刷されます*
                    シングルラインループ、
 declare -a listOfNames=('db_a' 'db_b' 'db_c')
 for databaseName in ${listOfNames[@]}; do echo $databaseName; done;次のような出力が得られます。
db_a
db_b
db_c私はgit pull更新のために私のプロジェクトの配列をループします:
#!/bin/sh
projects="
web
ios
android
"
for project in $projects do
    cd  $HOME/develop/$project && git pull
end