Subject: bug#50701: cannot append or insert to empty file or stream

From: lexi hale <[email-masked]>
Date: Mon, 20 Sep 2021 10:53:48 +0200

there is an inconsistency in the handling of the a and i commands in
sed. if the input stream or file immediately yields EOL, sed ignores
the commands and produces an empty stream.

for instance:

	$ echo -n >file line-1
	$ sed -i aline-2 file
	$ cat file

correctly yields

	line-1
	line-2

however,

	$ touch empty
	$ sed -i aline-1 empty
	$ cat empty

prints nothing,  instead of the expected result "line-1".

this is an extremely surprising behavior that limits the utility of sed
when one cannot predict the contents of the file in question.  if it is
strictly necessary for standards conformance or to meet the
expectations of ancient shell scripts that depend on nonstandard
behavior, it would be helpful to at least have a flag that would turn
on consistent behavior for these commands.

however, making the consistent behavior the new default might also fix
a few extremely rare bugs in existing shell scripts :)

thanks for your time,
lexi