Skip to content

/compat/sed/gnu-vs-bsd-sed

sed -i on macOS vs Linux: GNU sed vs BSD sed

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · sed (GNU sed) 4.9updated 2026-10-20
tool
sed
versions
GNU sed 4.x, BSD sed (macOS)
os
linux, macos

sed -i 's/a/b/' file edits a file in place with GNU sed (Linux), but fails or misbehaves with BSD sed (macOS), where -i requires a backup suffix argument: use sed -i '' 's/a/b/' file on macOS. The portable form for both is sed -i.bak 's/a/b/' file && rm file.bak.

In-place editing

GoalGNU sed (Linux)BSD sed (macOS)
Edit in place, no backupsed -i 's/a/b/' fsed -i '' 's/a/b/' f
Edit in place, keep f.baksed -i.bak 's/a/b/' fsed -i .bak 's/a/b/' f or sed -i.bak ...
Portable for bothsed -i.bak 's/a/b/' f && rm f.baksame

On macOS, sed -i 's/a/b/' f takes s/a/b/ as the backup suffix and then treats f as the script, which fails with an error about the command.

GNU sed in place (run here)

bash
printf 'colour\nflavour\n' > words.txt
sed -i.bak 's/our/or/' words.txt
cat words.txt
ls words.txt*

Output

color
flavor
words.txt
words.txt.bak

Other differences

FeatureGNU sedBSD sed
Extended regex flag-E or -r-E
\n in the replacementnewlineliteral n (use a backslash-newline)
\+ and \? in basic regexsupported as GNU extensionsnot part of POSIX BRE; use -E with + and ?
--versionprints the versionunknown option

Use GNU sed on macOS

Install it with Homebrew (brew install gnu-sed); it is available as gsed, so scripts can call gsed -i explicitly.

Sources