Rationalizing sed -i/-I (in-place editing) argument handling

John-Mark Gurney jmg at funkthat.com
Mon Mar 8 22:00:35 UTC 2021


Ed Maste wrote this message on Mon, Mar 08, 2021 at 15:13 -0500:
> A relatively minor but longstanding incompatibility between FreeBSD
> and many other systems is the way sed handles backup files for
> in-place editing -- sed's -I and -i options. GNU sed and other BSDs
> accept an optional argument: -I.bak will save a backup file with a
> .bak extension, and -I with no argument will not create a backup file.
> FreeBSD currently accepts either -I.bak or -I .bak to save a backup
> with the given extension, and -I "" (an empty argument) to specify no
> backup.
> 
> I've been tripped up by this in the past and I know many others have.
> Most recently tobik@  filed PR 254091 for this. Now, I think a single
> change to make -i/-I to be compatible with other sed implementations
> in one step is too much of a POLA violation, but I think it can
> reasonably be done in stages:
> 
> 1. Update the man page to indicate that -i/-I should not have a space
> between the flag and the extension. This is compatible with current
> FreeBSD sed, other BSDs sed, GNU sed, and my proposed changes below.
> No backup is still a special case and remains as -I "".
> 
> I've opened https://reviews.freebsd.org/D29128 with proposed man page changes.
> 
> 2. Continue accepting -I .bak, but emit a warning suggesting the use
> of -I.bak instead.
> 
> 3. Change -I/-i to getopt optional arguments, but keep a special case
> for -I/-i "". At this point -I .bak becomes invalid as with other
> seds, and specifying no backup can be done with either -I "" or -I
> with no argument. This relies on an empty argument having no other
> sensible interpretation.

I will say that IMO, I really dislike a command line argument having
an optional argument.  It can cause parsing confusion and possibly
security issues...  Copying this behavior, IMO, is not good...  So my
vote is against this misfeature..

How is the program suppose to tell when the extension is "-e", or -e
is a different option and not the argument to -I?

-- 
  John-Mark Gurney				Voice: +1 415 225 5579

     "All that I will do, has been done, All that I have, has not."


More information about the freebsd-hackers mailing list