sed: 1: invalid command code (BSD sed) macOS Origin: Platform & cloud OSI layer 7 · Application Last updated 5 Nov 2026

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

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

Exact message you may see sed: 1: invalid command codesed: 1: "file": invalid command code fsed: extra characters at the end of p commandsed: -i may not be used with stdinsed: RE error: illegal byte sequence
Seen on: macOS 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?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Platform & cloud. Errors that are often confused with or caused by this one (click to open):

command not found — bash: command not found (Linux)command not foundHomebrew Cask: It seems there is already an App at — Homebrew: Error: It seems there is already an App at '/Applications/Chrome.app' (macOS)Homebrew Cask: It see…Homebrew: Failed to update / fatal: unable to access — Homebrew: Error: Failure while executing; `git fetch --force origin` exited with 128 / fatal: not in a git directory (macOS)Homebrew: Failed to u…Homebrew: No available formula with the name — Homebrew: Error: No available formula with the name "x". Did you mean ...? (macOS)Homebrew: No availabl…illegal option on macOS (date -d, readlink -f, grep -P) — macOS: date: illegal option -- d / readlink: illegal option -- f / grep: invalid option -- P (macOS)illegal option on mac…Your Command Line Tools are too outdated — Homebrew: Error: Your Command Line Tools (CLT) are too outdated. Update them from Software Update / Please update to the latest macOS (macOS)Your Command Line Too…zsh: bad CPU type in executable — macOS (Apple Silicon): zsh: bad CPU type in executable: ./tool (macOS)zsh: bad CPU type in …zsh: no matches found — zsh: no matches found: package[extra] (glob pattern not matched) (macOS)zsh: no matches foundsed: 1: i…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.