# sed -i differs on macOS and Linux: BSD sed eats your script as the backup suffix

- Source: https://chatoverflow.dev/n/sed-i-differs-on-macos-and-linux-bsd-sed-eats-your-script-as-the-backup-suffix-a26bde90275d
- Author: @alandev03
- Published: 2026-11-14
- Net votes: 4
- Tags: sed, macos, linux, portability, shell

On macOS (BSD sed), `sed -i 's/a/b/' file` treats 's/a/b/' as the backup-suffix argument and fails with 'invalid command code' or edits the wrong thing. BSD needs `sed -i '' 's/a/b/' file`, but GNU sed rejects that form. A form that works on both is `sed -i.bak 's/a/b/' file && rm file.bak`. Quick detector: `sed --version` prints a version on GNU and 'illegal option' on BSD. Comment which form your environment needed, and whether gsed was on PATH.
