sed -i on Mac vs Linux: Why In-Place Edits Break Scripts
A sed -i command that runs cleanly on Linux can fail on a Mac, and the reverse is just as true. GNU sed and the BSD sed that macOS ships read the argument after -i differently, and that one difference breaks scripts as they move between machines.
Below: both forms, a portable one, and what to check when an in-place edit misbehaves.
Quick answer
On macOS, -i takes the backup extension as a separate argument. To skip the backup, pass an empty string:
# macOS (BSD sed)
sed -i '' 's/foo/bar/g' notes.txtGNU sed on Linux takes the suffix attached to the option, and it is optional:
# Linux (GNU sed)
sed -i 's/foo/bar/g' notes.txtThe form both versions accept is a non-empty suffix attached to -i, followed by a cleanup of the backup:
sed -i.bak 's/foo/bar/g' notes.txt && rm notes.txt.bakIf you only need a quick change to a config file, a quick config edit in a terminal editor can be simpler than a one-liner.
Step-by-step
Find out which sed you have
Open the manual with man sed. On a Mac, the syntax line lists the option as -i extension, with the extension as its own argument, as the macOS sed reference on SS64 shows:
sed [-Ealnru] [-I extension] [-i extension] [file ...]On a Linux machine with GNU sed, the manual describes -i with an optional suffix attached to the flag. If a script of yours runs on both, plan for both.
Use the macOS form
BSD sed expects the extension right after -i. If you give it an empty string, no backup is saved. The same page uses this example:
sed -i '' -e 's/foo/bar/g' test.txtThe page describes it as replacing every foo with bar in the file without creating a backup. A non-empty extension, such as -i .bak, saves the original under that extension.
Use the GNU form
GNU sed treats the suffix as part of the option, so -i alone edits the file and keeps no copy, and -i.bak keeps a .bak copy. A separate empty argument is not a suffix there; GNU sed reads it as the next argument, which is the script. That is why the macOS idiom -i '' fails on Linux.
Pick a portable form
If the script has to run in both places, attach a non-empty suffix and delete the backup afterward:
sed -i.bak 's/foo/bar/g' config.txt
rm config.txt.bakIt leaves a short-lived backup, but it skips the platform check. If you'd rather not leave a file behind, write the result to a temporary file and move it into place:
sed 's/foo/bar/g' config.txt > config.txt.tmp && mv config.txt.tmp config.txtInstall GNU sed as gsed
The SS64 page notes that many of the utilities shipped with macOS are very old versions and suggests installing GNU sed with Homebrew:
brew install gnu-sedIt installs as gsed, so the system sed stays as it was. Call gsed -i 's/foo/bar/g' file in scripts that were written for Linux. If you set up Macs often, a Brewfile that keeps your installed tools in one text file makes gnu-sed part of the setup.
Common problems and fixes
The error points at the wrong thing
Run the GNU form on macOS and BSD sed takes your script as the backup extension, then takes the file name as the script. The error you see is about a script or a missing file, not about -i. So when a Linux command fails on a Mac with a message that makes no sense, check the argument after -i first.
A zero-length extension is risky
An empty extension means no backup. The FreeBSD sed manual says so, and adds that it is "not recommended to give a zero-length extension when in-place editing files, as you risk corruption or partial content in situations where disk space is exhausted." For a file you cannot recreate, keep a backup suffix and delete it only after you have checked the result.
-i is not part of the standard
The same man page lists -E, -I, -a and -i as non-standard FreeBSD extensions that may not be available on other operating systems. So -i is a vendor feature everywhere it works, and a script that uses it is leaning on one. The -r option is documented on the BSD side as the same as -E, kept for compatibility with GNU sed, so extended regular expressions are less of a problem than -i. Regular expressions still differ in other ways between tools; see how regex flavors differ between tools.
Several files are edited independently
With -i, BSD sed treats each file on its own: line numbers restart at 1 in each file, and $ matches the last line of the current file. The -I option instead treats all files as one continuous run of lines. For most edits, -i is the one you want.
Check what changed
After an in-place edit on a tracked file, look at the difference before you move on. Reading a unified diff takes a minute and catches a substitution that matched more than you meant.
Doing this with Carets
If the change is a one-off, you don't need a one-liner at all. Carets is a native, fast notes and code editor for iPhone, iPad, and Mac, so a config file, a script, or a plain-text note opens in the same place on any of them. It highlights the syntax of code and config files, so a stray character in a shell script stands out. Search and replace works on the text you are looking at, and you see the change before you save it.
Your files stay plain text, organized into files, projects, and tags, so a script you edit in Carets is still the same portable file you run in a terminal. Use sed for repeatable edits across many files, and an editor for the change you'll make once.
Download Carets on the App Store for iPhone, iPad, and Mac.
Frequently Asked Questions
Why does sed -i fail on a Mac?
BSD sed, which macOS ships, reads the argument after -i as the backup extension. In the GNU form the script lands in that slot, so sed misreads the script and the file name. Pass an extension as a separate argument, or an empty string for none.
Is sed -i '' safe on Linux?
No. GNU sed takes the suffix attached to the option, so a separate empty argument is read as the script. Use a non-empty attached suffix such as -i.bak for a form both accept.
Is -i part of the POSIX standard?
The FreeBSD man page lists -i as a non-standard extension that may not be available on other systems, so any script that relies on it relies on a vendor feature.
How do I get GNU sed on a Mac?
Install it with Homebrew using brew install gnu-sed. It runs as gsed, so the macOS sed stays untouched.