sed -i empty argument compatibility issue

Gary Aitken freebsd at dreamchaser.org
Sat Mar 6 05:27:43 UTC 2021


On 3/5/21 9:33 PM, parv/freebsd wrote:
<snip>
>      > 3. Avoid -i and use a temporary file, just like in the old days.
>      > However this introduces the need for temporary file handling.  But
>      > maybe that isn't important here.
>      >
>      > sed "/^# /d" "${outfile}" > "$tmpfile" && mv "$tmpfile" "${outfile}"
> 
> 
> This won't do, when ...
> 
>      > 4. Introduce a "sed" wrapper in PATH that intercepts the call to the
>      > real sed, detects this problematic usage case, and then DTRT does
>      > the right thing with it.  This wrapper could have all of the correct
>      > temporary file handling needed to make this work.
> 
> ...
> 
> #!/bin/sh
> 
> # Set sed path & -i option based on "uname -a" output.
> SED=
> host_type=$( uname -a )
> case "${host_type}" in
>    *Linux* )
>      SED="/usr/bin/sed -i''"
>    ;;
>    *FreeBSD* )
>      SED="/usr/bin/sed -i ''"
>    ;;
> esac
> 
> # Set $@ to contain original arguments prefixed by sed with -i option.
> set -- ${SED} $@
> # Now run the sed with the arguments given.
> $@

Unfortunately, that involves adding a script that then has to be present
on every system where the original software package is to be used, or
incorporating it into the package itself.  Which adds confusion for anyone
working with other packages, which may or may not assume the presence of
the script.

>      > 6. Compile GNU sed locally and install it in PATH earlier.  This
>      > would probably be a generally good option globally.  However it's
>      > possible you might run into portability problems in the reverse
>      > case.
> 
> 
> If GNU sed could be installed, this would be most agreeable.

But wouldn't that potentially break other ports which assume fbsd sed,
and may have patches assuming that?  Or system scripts which assume fbsd
sed?

> I prefer to use the absolute path to remove ambiguity about the kind of
> sed being used, especially in the above case.

for sure; that would get around the "which sed" issue, although it would
require setup in the Makefile, which wouldn't be a big deal.  But it would
also require changing every "sed" to "$SED" in the package.

Perhaps SED should be one of those variables that appear in all the cmake
files that I don't understand...

Gary


More information about the freebsd-questions mailing list