So, I have two arrays:
arr1=("A" "C" "E")
arr2=("B" "D" "F")
I have nested loops like this:
for i in $(seq 1 $numberOfYears);
do
echo "$i"
for j in "${arr1[@]}";
do
echo ${arr1[j]} <---Keeps printing "A"
echo ${arr2[j]} <---Keeps printing "B"
done
done
New to programmatic shell scripting. What would cause the inner for loop to correctly iterate through array1
when you reference the element like this: $j but not like this ${arr1[j]} and ${arr2[j]} ..? In all of
my reading/searching, this should correctly iterate through both arrays.
My expected results:
Expected
1ABCDEF
2ABCDEF
3ABCDEF
Actual
1ABABAB
2ABABAB
3ABABAB
If I change ${arr1[j]} to $j it works fine, but I need to get the elements of arr2 as well, so I have to get it like ${arr2[j]}.
for j in "${!arr1[@]}"....declare -A arr=([A]=B [C]=D [E]=F); for k in "${!arr[@]}"; do echo $k ${arr[$k]} ; done.