Plongez dans un tableau de chaînes dans Bash

Mots clés : arraysbashshellarrays

meilleur 5 Réponses Plongez dans un tableau de chaînes dans Bash

vote vote

91

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

81

for databaseName in a b c d e f; do   # do something like: echo $databaseName done  
vote vote

79

#!/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=0; i<${arraylength}; i++ )); do   echo "index: $i, value: ${array[$i]}" done 
index: 0, value: one index: 1, value: two index: 2, value: three 
vote vote

63

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 -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 
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 
`  
`command`  
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[@]}" 
vote vote

57

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

Questions similaires