sed command creating unwanted duplicates of file with -e extension

On OSX sed (BSD) sed requires an extension after -i option. Since it is finding -e afterwards it is adding -e to each input filename. btw you don't even need -e option here.

You can pass an empty extension like this:

sed -i '' 's/foo/bar/g' $file

Or use .bak for an extension to save original file:

sed -i.bak 's/foo/bar/g' $file

The accepted answer works for OSX but causes issues if your code is run on both GNU and OSX systems since they expect -i[SUFFIX] and -i [SUFFIX] respectively.

There are probably two reasonable solutions in this case.

  1. Don't use -i (inplace). Instead pipe to a temporary file and overwrite the original after.
  2. use perl.

The easiest fix for this I found was to simply use perl. The syntax is almost identical:

sed -i -e 's/foo/bar/g' $file

->

perl -pi -e 's/foo/bar/g' $file