PS Although you wrapped my parameter expansion in a function like this
if you want to concatenate arguments like that before quoting, you can just
quote() {
printf "'%s'\n" "${*//\'/\'\'}"
}
The set -- "$*"
won’t be crazily expensive, but I bet it’s over 10% of the cost of the whole function when the other operations as cheap as these.
For me this function runs in around 11us on your Don't Let's Start
string whereas my POSIX one
quotep() {
set -- "$*" ""
while [ "${1#*\'}" != "$1" ]; do
set -- "${1#*\'}" "$2${1%%\'*}''"
done
printf "'%s'\n" "$2$1"
}
takes around 40us. I suspect that my linking against musl rather than glibc will skew the costs of some string operations vs others, so YMMV.
And of course, none of this matters if it end up in a $(...)
construct anyway.