# macOS: sed: 1: "file.txt": invalid command code f / extra characters at the end of p command

**Code:** `sed: 1: invalid command code (BSD sed)` · **Category:** macOS · **Seen on:** macOS, Linux

macOS ships BSD `sed`, where `-i` requires a backup-suffix argument, unlike GNU sed on Linux.

## Meaning

Use `sed -i '' 's/a/b/' file` on macOS (empty string = no backup) or `sed -i.bak ...` for both. For scripts that must work on Linux and macOS use `perl -pi -e`, or install GNU sed (`brew install gnu-sed`, command `gsed`).

## Common causes

- GNU-style `sed -i 's/a/b/' file` on macOS
- Missing empty suffix argument
- `\n`/`\t` escapes not supported in BSD replacement
- Script written for Linux
- `-r` option (use -E)

## Quick fix

1. `sed -i '' 's/a/b/' file` on macOS
2. Portable: `sed -i.bak 's/a/b/' file && rm file.bak`
3. `brew install gnu-sed` and use `gsed`

## Detailed fix by platform

### macOS

1. `sed -i.bak 's/foo/bar/g' file && rm file.bak` works on both systems

## How to diagnose

1. **OS** — macOS (BSD)?
2. **Flag** — -i suffix?
3. **Portable** — perl or gsed?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/macos/bsd-sed-invalid-command-code)
