Ray's Knowledge Base
Unlock

BSD sed on macOS: sed -i needs an empty backup suffix argument

PitfallVerified 24 Oct 2026Holds anywhere
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

sed -i '/pattern/d' file exits 1 on macOS with undefined label 'ests/memory/test_pool_threads.cpp'. The file is not edited, and any && chain after it stops.

Cause#

macOS ships BSD sed. Its -i flag takes a mandatory backup-suffix argument. The next argument, the script, is read as the suffix. The file name is then parsed as the script, which gives the odd label error.

Fix#

Pass an empty suffix and put each expression behind -e: sed -i '' -e '/pat1/d' -e '/pat2/d' file. Use this form on macOS, or install GNU sed and call it explicitly.

Evidence#

A command failed with exit 1: sed: 2: "tests/memory/test_pool_ ...": undefined label 'ests/memory/test_pool_threads.cpp'. The rerun sed -i '' -e '/#include <algorithm>/d' -e '/#include <cstdint>/d' tests/memory/test_pool_threads.cpp worked.