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

Actual output:

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

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.

  • Looks as if sed creates the temporary file with the proper output but quits before it moves the file into place. The following does the same thing as you expect the i option to do- sed /foo/q < test > test.tmp && mv test.tmp test – fd0 May 28 at 13:32
  • @fd0 Yes, that’s exactly what I’m currently doing. It’s still confusing, and GNU sed behaves differently. So, a bug? – Konrad Rudolph May 28 at 13:52

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .