Why not use bash's printf %q?
string=$(printf ' %q' "${array[@]}") string=$(printf ' %q' "${array[@]}") function quote() {
echo "${@@Q}"
}
(In both cases, the generated output is understood bash, but not necessarily by other POSIX-compliant shells.)