Issue
I wants to loop thru two strings that has the same delimiter and same number of items in bash shell.
string1=s1,s2,s3,s4
string2=a1,a2,a3,a4
IFS=','
count=0
for i in ${string1[@]}
do
echo $i
echo $count
// how can I get each item in string2?
count=$((count+1))
done
As far as I know, I cannot loop thru two string at the same time. How can I get each item in string 2 inside the loop?
Solution
Extract the fields of each string into separate arrays, then iterate over the indices of the arrays.
IFS=, read -ra words1 <<< "$string1"
IFS=, read -ra words2 <<< "$string2"
for ((idx = 0; idx < ${#words1[@]}; idx++)); do
a=${words1[idx]}
b=${words2[idx]}
echo "$a <=> $b"
done
s1 <=> a1
s2 <=> a2
s3 <=> a3
s4 <=> a4
Answered By - glenn jackman Answer Checked By - Clifford M. (WPSolving Volunteer)