Console9

GNU sed vs BSD sed: key differences

Compare GNU sed (Linux) and BSD sed (macOS) for in-place editing, regex syntax, and portability.

GNU sed vs BSD sed: key differences

GNU sed (Linux) and BSD sed (macOS) differ in in-place editing syntax, regex support, and flag behavior.

How GNU sed Handles In-Place Editing

GNU sed accepts -i without an argument to edit files in place without creating a backup. GNU sed also accepts -i.bak to create a backup with the .bak extension. The -i flag and the backup extension are a single argument.

How BSD sed Handles In-Place Editing

BSD sed requires a separate argument after -i— even if the argument is an empty string. sed -i '' 's/old/new/' file edits in place without a backup. sed -i .bak 's/old/new/' file creates a backup. Omitting the argument after -i causes BSD sed to interpret the next argument as the backup extension.

Feature Comparison: GNU sed vs BSD sed

FeatureGNU sedBSD sed (macOS)
In-place edit without backupsed -i 's/...'sed -i '' 's/...'
Extended regex flag-r or -E-E only
\t in replacementSupported (inserts tab)Not supported (inserts literal \t)
\n in replacementSupported (inserts newline)Not supported
Case-insensitive flags/pattern/repl/INot supported

How to Write Portable sed Commands

Use -E (not -r) for extended regex. Avoid \t and \n in replacements. For in-place editing, test the command without -i first, then use the platform-appropriate flag. For fully portable scripts, install GNU sed on macOS via Homebrew ( brew install gnu-sed).