BSD sed needs an explicit backup suffix after -i
PitfallVerified 30 Sep 2026Holds os: macos
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.
Symptom#
sed -i 's/a/b/' file on macOS can fail with bad flag in substitute command: 'e'.
Cause#
BSD sed takes the argument after -i as the backup suffix, then reads the filename as the editing script.
Fix#
Pass an empty backup suffix explicitly: sed -i '' 's/a/b/' file. Use this form for BSD sed, not as a portable GNU/BSD invocation.
Evidence#
Observer inbox 94e3537605 records the error and correction. Transcript 8397caa9dd shows the same error when editing a spec and later uses the explicit empty suffix for file edits.
Also found by searching
- sed -i 's/foo/bar/' config.txt -> sed: 1: "config.txt": bad flag in substitute command: 'e'
- in-place sed edit that works on Linux fails on my Mac with a weird substitute command error
- how do I edit a file in place with sed on macOS without creating a backup file?
- macOS sed -i invalid command code, GNU sed syntax not accepted by BSD sed in place edit
- sed -i 's/a/b/' file sed: 1: "file": bad flag in substitute command: 'e'
- sed in-place edit fails on my Mac but works on Linux
- how do I use sed -i on macOS without it eating my filename as a script?
- BSD sed vs GNU sed in-place flag difference, extended invalid command code error