Example 0
names=( Jennifer Tonya Anna Sadie )
This creates an array called names with four elements (Jennifer, Tonya, Anna, and Sadie).
names=( "John Smith" "Jane Doe" )
This creates two array elements, each containing a space.
colors[0] = red
colors[3] = green
colors[4] = blue
This declares three elements of an array using nonsequential index values and creates a sparse array (there are no array elements for index values 1 or 2).
filearray=( `cat filename | tr '\n' ' '`)
This example places the contents of the file filename into an array. The tr command converts newlines to spaces so that multiline files will be handled properly.
names=( "${names[@]}" "Molly" )
This example adds another element to an existing array names.
Example 1
#existing array
existing=(item1, item2, item3);
#items to merge (including an item requiring correct quoting)
merge=(item4 item5 "${item6}*");
count=${#existing[@]};
num_new_items=${#merge[@]};
# this loop appends items to the end of the array
for (( i=0;i<$num_new_items;i++)); do
echo ${i};
existing[$count]=${merge[${i}]};
let count+=1;
done
count=${#existing[@]}
for (( i=0;i<$count;i++)); do
echo ${existing[${i}]};
done
Example 2
cat $file | while read line; do
echo "$line";
done