Copy a Bash array with empty elements
You have a quoting problem and you should be using @
, not *
. Use:
copy=( "${array[@]}" )
From the bash(1)
man page:
Any element of an array may be referenced using
${name[subscript]}
. The braces are required to avoid conflicts with pathname expansion. Ifsubscript
is@
or*
, the word expands to all members ofname
. These subscripts differ only when the word appears within double quotes. If the word is double-quoted,${name[*]}
expands to a single word with the value of each array member separated by the first character of theIFS
special variable, and${name[@]}
expands each element ofname
to a separate word.
Example output after that change:
--- array (3) ---onethree--- copy (3) ---onethree