“sed: -I or -i may not be used with stdin” is an error that may occur on MacOS, or other systems that have non-standard implementations of GNU sed.

Fix sed: -I or -i may not be used with stdin

Fix sed -i may not be used by stdin by changing -i to -i.backup

To fix this error you must pass -i an argument telling sed to create a backup of the original file before overwriting. In the above example, -i.backup means: overwrite rhyme.txt, but create a backup called rhyme.txt.backup first. You may specify any file-extension you want (-i.original, -i.bak, -i.v1 etc are all valid file-extensions).

According to the GNU sed official documentation, when -i is passed without a file-extension, the original file will be overwritten without a backup; however, MacOS’s implementation doesn’t allow overwriting the original file without a backup. For a more in-depth explanation, see the next section.

What does sed: -I or -i may not be used with stdin mean?

-i is short for --in-place and is a command-line option that tells sed to edit the file(s) in-place (overwrite the original file) instead of printing the results to standard out. In most linux distros or unix-like systems that implement sed, -i also takes an optional argument which tells sed to create a backup of the original file before overwriting. This optional argument is the file-extension of that backup file.

Examples

Overwrite “rhyme.txt” with the results of the sed operation:

sed -i 's/Jack/Jill/g' rhyme.txt

Overwrite “rhyme.txt”, but make a backup called “rhyme.txt.backup” first:

sed -i.backup 's/Jack/Jill/g' rhyme.txt

Note: -i.backup is short for --in-place=.backup.

So what’s the problem?

If no extension is supplied to -i, the original file is overwritten without making a backup; however, in MacOS’s implementation of sed, -i’s file-extension argument is mandatory. In other words, when using sed on MacOS you cannot overwrite the original file without creating a backup.

So to fix “sed: -I or -i may not be used with stdin”, you must specify a file-extension: -i.backup instead of just -i.