Reputation: 1215
I wrote a function which returns an array:
create(){
my_list=("a" "b" "c")
echo "${my_list[@]}"
}
result=$(create)
for var in result
do
echo $var
done
Now, I'd like to extend it in order to return multiple arrays. For example, I'd like to write something like that:
create(){
my1=("1" "2")
my2=("3","4")
my3=("5","6")
echo "${my1[@]} ${my3[@]} ${my3[@]}"
}
and I'd like to get each of the returned arrays:
res1= ...
res2= ...
res3= ...
Anyone could suggest me a solution? Thanks
Upvotes: 1
Views: 423
Reputation: 532418
You need to read with a while
loop.
while read -r val1 val2; do
arr1+=("$val1")
arr2+=("$val2")
done < file.txt
There is no such thing as an array value in bash
; you cannot use arrays the way you are attempting in your question. Consider this result:
create(){
my_list=("a 1" "b 2" "c 3")
echo "${my_list[@]}"
}
result=$(create)
for var in $result
do
echo $var
done
This produces
a
1
b
2
c
3
not
a 1
b 2
c 3
Upvotes: 1