On Fri, May 6, 2011 at 12:00 PM, Greg Wooledge <wool...@eeg.ccf.org> wrote:
> On Fri, May 06, 2011 at 11:50:34AM -0400, Chet Ramey wrote: > > Eric suggested %q, and that works to a > > certain degree, but you can also use > > > > printf '"%s" ' "${vals[@]}" ; echo > > > > and get the double-quoting you want. > > Fails horribly if the array elements contain double quotes of their own. > > Realistically, I do NOT advise even attempting this. Do not try to write > general functions that operate on arrays, passed by name. Bash truly > does not have the tool set to cope with this. I can't stress this > strongly enough. > > The only possible way I can think of to do this safely in bash is to do something like: eval "printf '%s\0' \"\${$1[@]}\"" and then loop over the NUL-delimited output.