If you have an array and just want to pass the elements intact to a command, it's enough to use $array in zsh, or "${array[@]}" (with quotes!) in Bash/ksh. The former drops empty elements, though, but "${array[@]}" works in either:
zsh:
% l=("foo * bar" '' $'new\nline') % printf '<%s>\n' $l <foo * bar> <new line> % printf '<%s>\n' "${l[@]}" <foo * bar> <> <new line> Bash:
$ l=("foo * bar" '' $'new\nline') $ printf '<%s>\n' "${l[@]}" <foo * bar> <> <new line> On the other hand, if you need to pass the array elements to a shell to be executed as a command line, you'll need to quote them properly. This would happen if you need to embed the values in a shell script, or run them through eval (for whatever reason), or pass them through ssh.
Here, you can use printf %q in Bash or zsh, or the (q) modifier in zsh.
Bash's printf %q tends to use backslashes, so the output is ugly, but it should work (the echo is to add a trailing newline for the printout):
$ printf '%q ' "${l[@]}"; echo foo\ \*\ bar '' $'new\nline' zsh's (qqqq) uses the $'...' quoting which looks nicer (opinion, of course):
% printf '%q ' "${l[@]}"; echo foo\ \*\ bar '' new$'\n'line % printf "%s " ${(qqqq)l}; echo $'foo * bar' $'' $'new\nline' However, note that $'...' is nonstandard and may have differences between shells.
See: Escape a variable for use as content of another script
In any case, I would try to avoid inserting quoted data inside code. It's usually a pain and has loads of corner cases.
In most cases with filenames, it's enough to pass them raw, one on each line and read them into an array. (And make sure you don't have newlines in the filenames, or check for them and complain.)