How to copy an array in Bash?
The solutions given in the other answers won't work for associative arrays, or for arrays with non-contiguous indices. Here are is a more general solution:
declare -A arr=([this]=hello [\'that\']=world [theother]='and "goodbye"!')temp=$(declare -p arr)eval "${temp/arr=/newarr=}"diff <(echo "$temp") <(declare -p newarr | sed 's/newarr=/arr=/')# no output
And another:
declare -A arr=([this]=hello [\'that\']=world [theother]='and "goodbye"!')declare -A newarrfor idx in "${!arr[@]}"; do newarr[$idx]=${arr[$idx]}donediff <(echo "$temp") <(declare -p newarr | sed 's/newarr=/arr=/')# no output