On Wed, 20 Jul 2022 at 17:30, Eric Blake <ebl...@redhat.com> wrote: > > On Wed, Jul 20, 2022 at 04:26:29PM +0100, Peter Maydell wrote: > > The variable string-replacement syntax ${var/old/new} is a bashism > > (though it is also supported by some other shells), and for instance > > does not work with the NetBSD /bin/sh, which complains: > > ../src/configure: 687: Syntax error: Bad substitution > > > > Replace it with a more portable sed-based approach, similar to > > what we already do in quote_sh(). > > > > > for e in $1; do > > - e=${e/'\'/'\\'} > > - e=${e/\"/'\"'} > > - printf '"""%s""",' "$e" > > + printf '"""' > > + # backslash escape any '\' and '"' characters > > + printf "%s" "$e" | sed -e 's/\([\"]\)/\\\1/g' > > You've fixed the bashism, but at the expense of a non-POSIX use of > sed. POSIX says the input to sed must be a text file (ending in a > newline; but $e does not), and as a result it always outputs a newline > (but you don't want a newline before the closing """). GNU sed > happens to do what you want for input not ending in a newline, but I > don't remember off-hand whether BSD sed does, and I know that Solaris > sed does not.
I just copied the approach we already take in quote_sh: quote_sh() { printf "%s" "$1" | sed "s,','\\\\'',g; s,.*,'&'," } Is that also relying on this non-portable sed use? > If this passes on BSD, then I'm okay with it; but if we want to avoid > non-POSIX altogether, this should work (using the shell's $() to strip > the trailing newline we added to keep sed happy): > > # backslash escape any '\' and '"' characters > printf '"""%s""",' "$(printf "%s\n" "$e" | sed -e '/s/\([\"]\)/\\\1/g')" Mmm. -- PMM