The problem is that sed
is a stream editor, therefore in-place editing is a non-POSIX extension and everybody may implement it differently. That means for in-place editing you should use ed
for best portability. E.g.
ed -s foobar.txt <<<$',s/foo/bar/g\nw'