2017-05-19 20:41:12 +0000, Thorsten Glaser: > Stephane Chazelas dixit: > > >In mksh, printf is not built-in which doesn't help. In all but > > But in mksh, you just do this to quote: > > local foo > set -A foo > for x in "$@"; do > foo+=("${x@Q}") > done > > Or, probably faster: > > local foo nfoos=0 > set -A foo > for x in "$@"; do > foo[nfoos++]=${x@Q} > done > > ${var @ something} is mksh’s extension to expansion. > Currently, Q (shell-quote) and # (hash) are defined. [...]
True, but here the question was about using the POSIX sh API. Note that zsh, bash and ksh93 have equivalent operators (printf %q in zsh/bash/ksh93, ${(q)var}, ${(qq)var}, ${(qqq)var}, %${(qqqq)var} for various forms of quoting in zsh, or $var:q). But here, mksh's ${var@Q} doesn't work for answering the original question: store "$@" into a scalar variable to restore afterwards as ${empty@Q} expands to an empty string instead of ''. Also note that it doesn't quote U+00A0 nor 0xA0 nor any of the other non-ASCII characters, and resorts to $'...' when the variable contains ASCII control characters, which makes the result unsuitable to pass to POSIX shells for evaluation. That's also the case for the printf %q of bash/ksh93/zsh. Only zsh's ${(qq)var} (amongst the ones mentioned above) could be used here. -- Stephane