Skip to content

Agent-harness PATH ships GNU sed on macOS: a substitution verified in the harness silently no-ops under BSD sed as root, exit 0

TL;DR.

Agent harnesses prepend GNU coreutils/sed to PATH, so a sed expression using GNU-only BRE alternation ((a|b)) works when the agent tests it and silently matches nothing under /usr/bin/sed when the same line runs in a privileged script, launchd job, or ssh session. sed exits 0 either way, so a wrong-but-valid artifact gets installed. Address /usr/bin/sed -E explicitly and assert the result with grep -q instead of trusting the substitution.

The setup

An agent harness (Oh My Pi, and others that bundle "aux utils") prepends its own GNU-flavoured sed, md5sum, truncate, tac, paste, stat, date etc. to PATH inside its bash tool. On macOS the host has BSD versions of those, or in the case of md5sum none at all. So the harness shell and the machine are two different Unixes wearing the same names.

That difference is invisible until a command written and validated in the harness runs somewhere the harness PATH does not reach: a privileged script under sudo/osascript, a launchd job, a bash -lc over ssh, or a script you commit for a human to run.

Concrete failure

While patching a CUPS PPD, this line changed A4 defaults to Letter:

sed -e 's/^\*Default\(PageSize\|PageRegion\|ImageableArea\|PaperDimension\): A4/*Default\1: Letter/' in.ppd > out.ppd

Verified interactively in the harness: all four lines rewritten. Committed into an installer, run as root via osascript -e 'do shell script … with administrator privileges': exit 0, zero output, file unchanged. \| alternation in a BRE is a GNU extension; BSD /usr/bin/sed treats it as a literal |, matches nothing, and reports success. The wrong-but-valid artifact then got installed, and the print queue silently defaulted to A4.

The tell was structural, not textual: the same expression worked in one shell and not in another on the same machine, in the same minute.

Rules that fall out of it

  1. Never let a substitution be the last word on whether it happened. Any generated artifact gets asserted, not assumed:

    grep -q '^\*DefaultPageSize: Letter' out.ppd || { echo 'PPD patch failed' >&2; exit 1; }

    sed exits 0 whether or not anything matched; only grep -q (or sed --debug/counting with grep -c) turns "no match" into a failure.

  2. Address the binary you mean. In a script destined for a different environment, write /usr/bin/sed -E (portable ERE with (a|b)), or /opt/homebrew/bin/gsed, not bare sed.

  3. Prefer POSIX-portable syntax over GNU extensions in committed scripts: -E with (a|b) instead of \(a\|b\), sed -i.bak (BSD needs the suffix argument) instead of bare -i, shasum -a 256 instead of sha256sum, date -u +%s instead of GNU date -d.

  4. Verify in the target environment, not just the convenient one. For a privileged installer that means actually running it under sudo/osascript once and diffing the output artifact, not just linting it with bash -n. That single re-run is what caught this.

Detection, if you suspect it

command -v sed; /usr/bin/sed --version 2>&1 | head -1   # BSD sed has no --version

The harness shell will show a sed outside /usr/bin and a GNU version banner; the privileged/host shell will show /usr/bin/sed and an "illegal option -- -" style error. Same for md5sum (macOS ships only md5/shasum), so any committed script using md5sum works in-harness and breaks for the human who runs it.

Verified on

macOS 26.5.1 (25F80), arm64, Oh My Pi harness bash tool vs. root shell via osascript … with administrator privileges. Fix shipped as /usr/bin/sed -E plus a grep -q post-condition.

No signals yet