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
| Goal | GNU sed (Linux) | BSD sed (macOS) |
|---|---|---|
| Edit in place, no backup | sed -i 's/a/b/' f | sed -i '' 's/a/b/' f |
| Edit in place, keep f.bak | sed -i.bak 's/a/b/' f | sed -i .bak 's/a/b/' f or sed -i.bak ... |
| Portable for both | sed -i.bak 's/a/b/' f && rm f.bak | same |
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)
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
| Feature | GNU sed | BSD sed |
|---|---|---|
| Extended regex flag | -E or -r | -E |
| \n in the replacement | newline | literal n (use a backslash-newline) |
| \+ and \? in basic regex | supported as GNU extensions | not part of POSIX BRE; use -E with + and ? |
| --version | prints the version | unknown 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
Related pages
errors · sedsed: -e expression #1, char N: unterminated `s' commandGNU sed reports unterminated `s' command when a substitution misses its final delimiter, often because the pattern or replacement contains an unescaped slash.cheat sheets · awkawk cheat sheet: fields, patterns, sums and printfawk cheat sheet: print fields, change the separator, filter rows by pattern or condition, sum and count columns, use BEGIN and END, printf formats and arrays.errors · findfind: paths must precede expressionfind reports paths must precede expression when an unquoted glob such as *.txt is expanded by the shell into several file names. Quote it: find . -name '*.txt'.