How to merge two arrays in a zipper like fashion in Bash?

后端 未结 5 1848
野趣味
野趣味 2020-11-29 13:23

I am trying to merge two arrays into one in a zipper like fashion. I have difficulty to make that happen.

array1=(one three five seven)
array2=(two four six          


        
5条回答
  •  情书的邮戳
    2020-11-29 13:57

    Assuming both arrays are the same size,

    unset result
    for (( i=0; i<${#array1[*]}; ++i)); do
        result+=( "${array1[$i]}" "${array2[$i]}" )
    done
    

提交回复
热议问题