Duplicating an array 1:1 in zsh

Viewed 500

Although it seems fairly simple, there is a small nuance to the obvious solution.

The following code would cover most situations:

arr_1=(1 2 3 4)
arr_2=($arr_1)

However, empty strings do not copy over. The following code:

arr_1=('' '' 3 4)
arr_2=($arr_1)
print -l \
  "Array 1 size: $#arr_1" \
  "Array 2 size: $#arr_2"

will yield:

Array 1 size: 4
Array 2 size: 2

How would I go about getting a true copy of an array?

1 Answers
Related