2017-05-17 14:12:28 +0700, Robert Elz:
[...]
> | Depends. If quoting only a handful a arguments, then that call
> | to awk might cost you you a couple of milliseconds indeed. But
> | if processing thousands, you might find that it saves a few
> | seconds.
>
> And if I really had an application, whose only purpose was to quote
> huge numbers of strings, and do nothing else, then I'd write it in a
> proper compiled language, and it would be even faster. But I don't.
[...]
The initial question was about round-tripping "$@", so an
arbitrary number of arguments.
You don't need to quote a *single* argument to round-trip it. The
point of quoting those strings is so that you can save the list
in a scalar variable, for instance because your shell doesn't
support arrays or because you want to pass them in an
environment variable or you want to pass them to a remote command
over ssh (and can assume the remote shell is Bourne-like).
In another email in this thread, you said:
> Personally I have never (I think, not once) ever really thought
> that I would prefer a shell that had an "array" data type,
> they're really just not required.
>
Let me just offer a different opinion. To me, a shell doesn't
need any other data type than "array"/"lists".
A shell is before all a command line interpreter. A command is
an array of arguments. A text file is an array of lines. A
directory is an array of files.
The main things a shell deals with are arrays.
"rc" (the plan9 shell, though I'm only familiar with the clone
for Unix) is the best design IMO. List is the only data type.
That's what variables store, that's what functions return there.
f=(*.txt foo bar)
ls -ld -- $f
cat -- $f > merge.txt
rm -f -- $f
I suspect you never use arrays because they are so awkward to
use in the "popular" Bourne-like shells that have them (ksh and
bash). You need "${f[@]}" instead of $f above which is a pain to
type on most keyboards.
The rc code above would work in zsh in that case. In zsh, you
still need "${f[@]}" (or "$f[@]" or "${@)f}") to preserve empty
elements of the array though.
fish like rc but with a different syntax (and also fixes the
issue with non-matching globs but in a different way from zsh)
set f *.txt foo bar
ls -ld -- $f
cat -- $f > merge.txt
rm -f -- $f
Even csh is not as bad as ksh:
set f = (*.txt foo bar) is a bit more awkward, but $f:q is
easier to type than "${f[@]}" (even if equally unintuitive).
--
Stephane