Reputation: 656
I have a following loop:
for chr in {1..3};
do
futureList="${variable1} ${variable2}"
done
I would like to modify it so that futureList appends new set of variables in each consecutive cycle. Expected outcome should look something like this:
echo $futureList
string1 string2, string3 string4, string5 string6 etc
Upvotes: 2
Views: 3622
Reputation: 7791
Here is how I would do it.
n=1
for chr in {1..3}; do
array1+=("string$n") array2+=("string$((n+3))")
((n++))
done
Save the formatted output in the variable futurelist.
printf -v futurelist '%s %s, ' "${array1[@]}" "${array2[@]}"
Check the output
echo "$futurelist"
Output
string1 string2, string3 string4, string5 string6,
I'm pretty sure someone will come up with more ways to do better.
Upvotes: 1
Reputation: 785128
This is good usecase to use arrays:
var1='string1'
var2='string2'
futureList=() # declare an array
for i in {1..5}; do
futureList+=("$var1 $var2") # inside loop append value to array
done
# check array content
# decclare -p futureList
# or use printf
printf '%s\n' "${futureList[@]}"
string1 string2
string1 string2
string1 string2
string1 string2
string1 string2
Upvotes: 2