---
title: Why my agent's shell commands kept failing on a Mac: 54 errors counted, then fixed
date: 2026-09-30
tags: [claude-code, macos, zsh, bash, gnu-coreutils, shell]
type: success
status: n/a
reproducible: true
visibility: public
stack: macOS (Apple Silicon) · Claude Code · Homebrew
agent: Claude Code
model: claude-opus-5-5
---

# Why my agent's shell commands kept failing on a Mac: 54 errors counted, then fixed

Plain version first: my coding agent writes shell commands for Linux, and my Mac is not Linux. Its shell is zsh, not bash, and its `sed`, `date` and `stat` are the old BSD versions, not the GNU ones the agent writes for. So a share of its commands fail, it retries, and I pay for both attempts.

I counted instead of guessing. Across 21 of my Claude Code sessions in one project: 44 `(eval):1: no matches found` errors (zsh refusing an unquoted `--include=*.md` before grep even runs), 6 `illegal option` errors from BSD `date` and `stat`, and 4 BSD `sed` parse errors. One of them happened while I was reading about the fix.

The fix is [Morten Larsen's claude-code-macos](https://github.com/Baune8D/claude-code-macos), explained in [his article](https://dev.to/baunegaard/why-claude-code-keeps-writing-shell-commands-that-fail-on-your-mac-3bea): two settings that make Claude Code run bash, plus a SessionStart hook that puts Homebrew's GNU tools first on the PATH, only for the agent. My own terminal keeps the macOS tools.

Before installing a stranger's hooks I ran [skill-xray](https://github.com/worklore/skill-xray) on the repo. It said T4, the highest tier, which looked alarming. Reading the files settled it: the scanner had matched prose in the README describing how Claude Code itself re-launches its binary as ugrep. The hooks append one `export PATH=` line to the file Claude Code provides for that purpose and print a hint if something is missing. Nothing else.

Then a before/after in fresh headless sessions, with the same five commands that had failed for me. Before: all five failed, and one worse than failed: `sed -i -e 's/old/new/' f.txt` exited 0 and left a stray `f.txt-e` behind. After: all five did what they say.

Two things I had predicted wrong. I expected my API keys to vanish, because they live in `~/.zshenv` and bash never reads it. They didn't: the agent's shell inherits Claude Code's own environment, which came from my zsh terminal. And the habit that does break is mine: I had been writing the BSD form `sed -i '' 's/a/b/' file`, and GNU sed rejects it. At least it fails loudly now instead of silently.

## Reproduce this
Prerequisites: a Mac with Homebrew and Claude Code; `jq` (for the hooks' hints).
Your agent will need from you: permission to edit `~/.claude/settings.json` (it adds an `env` block and a SessionStart hook group; back it up first) and to run `brew install`.
Steps:
1. Measure first. Count past failures in your own transcripts: `grep -ho '(eval):1: no matches found' ~/.claude/projects/*/*.jsonl | wc -l`, the same for `illegal option -- ` and `sed: 1: `.
2. Run skill-xray (https://github.com/worklore/skill-xray, `python3 scanner/scan.py <repo>`) on https://github.com/Baune8D/claude-code-macos, then read every hook it would run. Treat the tier as a reason to read, not as a verdict.
3. `brew install bash coreutils findutils gawk gnu-sed gnu-tar gnu-which grep`
4. Add `"env": {"CLAUDE_CODE_SHELL": "bash", "SHELL": "bash"}` to `~/.claude/settings.json`, and a SessionStart group running the three hooks from the repo's `.claude/hooks/` (`put-gnu-tools-on-path.sh`, `warn-shell-not-bash.sh`, `warn-old-bash.sh`) by absolute path. Or install the repo as a plugin and add only the `env` block by hand; a plugin can't set it.
5. Start a NEW session; settings are read at session start.
6. Stop writing BSD-only forms: `sed -i ''` fails under GNU sed. Use `sed -i 's/a/b/' file`.
Verify: in a fresh headless session (`claude -p "..." --allowedTools Bash < /dev/null`), have the agent run each as its own Bash call: `grep -rn x --include=*.md docs`, `sed -i -e 's/old/new/' f.txt && ls`, `date -d yesterday +%F`, `stat -c %s f.txt`, `ls */a.md nomatch*.txt`, and `echo "$BASH_VERSION"; sed --version | head -1`. All should work (the last `ls` reports the one missing file instead of aborting), no `f.txt-e` appears, bash is 5.x and sed is GNU. Your API keys should still be set: print their length, never their value.
