Why does sed -i create random files with “q” commands?

I’m aware that BSD sed behaves differently from GNU sed with regards to the -i option. However, the usual way of using sed -i '' on macOS ceases to work when using a q command, and I’m wondering whether this is a bug or whether I’m overlooking something in the documentation. In either case, I’d like to know how to restore the expected behaviour for the following simple command:

printf foo\\nbar > test
sed -i '' /foo/q test

Expected output:

$ ls -a
.    ..   test
$ cat test
foo

Actual output:

$ ls -a
.            .!79633!test ..           test
$ cat test
foo
bar
$ cat .\!79633\!test
foo

In other words: the original file is untouched. The result is instead written to a new, hidden file. The numeric value in the filename changes with every execution and looks like a process ID (it’s in the right range, too). Using q without an address gives equivalent behaviour.


Solution 1:

I’ve found confirmation in the commit history: it’s a bug, which was fixed in r338375 in August 2018.