Re: Document another portability pitfall of sed

Bruno Haible via Patches for autoconf - the GNU build system <[email protected]> Tue, 24 Jun 2025 22:06:56 +0200
Newsgroups gmane.comp.sysutils.autoconf.patches
Organization GNU
Message-ID <3708528.RrQHTzY0li@nimes>
Nick Bowler wrote:
> > Note: I think POSIX [1] mandates the behaviour of OpenBSD and Solaris sed.
> > But that is not immediately relevant for the Autoconf manual: The portability
> > problem exists regardless of how we might interpret POSIX.
> 
> The GNU behaviour is likely non-conforming with Issue 5 (1997) and
> earlier specifications, but since Issue 6 (2001) the meaning of \n in
> the replacement string is explicitly unspecified.  So both behaviours
> are POSIX-compliant nowadays.

After reading the POSIX spec again, I tend to agree (although there's still
some ambiguity).

> > +In the replacement text of an @samp{s}, it is not portable to use
> > +escape sequences like @samp{\n}.  While GNU @command{sed} interprets
> > +them, the @command{sed} commands of OpenBSD and Solaris don't.
> 
> It would probably be helpful to mention that the POSIX-standard and
> portable method to substitute a newline with sed is to use backslash-
> newline.  For example:
> 
> % echo foo | sed 's/f.*/line1\
> line2/'
> line1
> line2

That's correct. For newlines, this seems to be portable nowadays.

Bruno