I kept running into a small shell difference while working with a coding agent on a Mac, where the agent writes its commands like bash but they get run by my login shell, which is zsh. Most of the time nobody notices, since the two shells agree on almost everything an agent types. The few places where they don't agree are the annoying kind, because the command doesn't crash, it finishes fine and prints nothing, and "nothing" is a perfectly believable answer to a lot of questions.
The empty diff
The job was cherry-picking fixes from dev to a release branch of a component library. Before cherry-picking anything, we want to know whether the fix is already on the release, and comparing commit hashes doesn't help with that, since a cherry-picked commit lands under a different hash. So the agent compared file contents instead, which is the right idea:
F="src/Tabs/index.tsx src/Tabs/styles.module.css src/Tabs/Tabs.spec.tsx" git diff --stat origin/release origin/dev -- $F In bash, the unquoted $F is split on spaces, git gets three paths, and we see whatever differs. In zsh, unquoted variables are not split (the SH_WORD_SPLIT option is off by default), so git gets one path with spaces in it, which matches no file, and git diff happily reports no differences with exit code 0. The agent read the empty output as "these files are identical on both branches", and the next step would have been marking the ticket as "no changes needed", leaving the fix out of the release.
What saved it was a second check that disagreed. The release's first-parent history (git log --first-parent origin/release -- src/Tabs/) had no merge that could have brought the fix in, and "the files are identical, but nothing ever put the fix there" doesn't add up. Grepping the actual file content on each branch showed the fix wasn't on the release at all, and the diff with the paths written out came back with 208 added lines. The cherry-pick then also hit a real conflict, which is not what you'd expect for a fix that was "already there" :|
The same -- $F came back in a later cherry-pick session, with the same empty diff, and this time the agent went a step further in the wrong direction. The release file matched the version from before the fix, and since the diff said dev was the same as the release, it concluded dev must have lost the fix too and went looking for a revert there, before noticing that the empty diff was the shell again.
You can reproduce it in a few lines:
% git init -q demo && cd demo % echo x > a.tsx && echo y > b.tsx && git add . && git commit -qm init % echo z >> a.tsx % F="a.tsx b.tsx" % git diff --stat -- $F; echo "exit $?" exit 0 % git diff --stat -- ${=F} a.tsx | 1 + 1 file changed, 1 insertion(+) ${=F} is zsh's way of asking for the split explicitly. It works, but I'd rather not have the agent learn one more zsh-only syntax, so the version that stuck uses an array, which reads the same way in both shells as long as it's quoted:
files=(src/Tabs/index.tsx src/Tabs/styles.module.css src/Tabs/Tabs.spec.tsx) git diff --stat origin/release origin/dev -- "${files[@]}" When the list comes from another command, zsh can split the output on newlines with files=("${(@f)$(git diff --name-only <fix>^ <fix>)}"). In bash 4 and newer the same thing is mapfile -t files < <(...) (the bash that ships with macOS is 3.2 and doesn't have it), so this one is not portable, but at least it fails loudly in the wrong shell instead of returning nothing.
The other ones
A few more showed up over the next couple of weeks, all in the same family.
Unmatched globs abort the command. In zsh, a word with a * that matches no file is an error (NOMATCH), not a literal string like in bash. So grep -rn 'getLocalizationLabels' src --include=*.tsx never runs grep at all, zsh prints no matches found: --include=*.tsx, and if that line is part of a longer script, the rest of the script keeps going. In one review session, that left an empty section in the output, and the agent concluded the repo had no localized widgets, when there were localized widgets and a shared set of labels that already covered what the change needed. Quoting the pattern (--include='*.tsx') is the fix, and it's a good habit in bash too.
A colon after a variable is a modifier. zsh treats $var:h, $var:t, $var:s/a/b/ and friends as modifiers on the variable, even inside double quotes. Git uses ref:path syntax all the time, so something like this goes wrong in a way that's hard to guess from the error:
H=abc123 echo "$H:h/x" # prints ./x echo "$H:src/index.tsx" # fails with "bad substitution" echo "${H}:src/index.tsx" # prints abc123:src/index.tsx :h takes the "head" of the value, like dirname, so "$H:h/x" silently turns into ./x. :s is substitution and fails, which is at least an error. Braces around the name avoid all of it.
A word starting with = is a command lookup. I ran into this one myself while writing this post, after echo ===== failed with ==== not found, because zsh expands =cmd to the path of cmd. Quote it.
Newline-separated lists don't split either. A loop collected commit hashes into a variable, one per line, and passed it on unquoted, which in zsh is one argument with newlines in it. That one at least failed loudly, with File name too long, and the agent re-ran the loop under bash -c '...'. Wrapping a script in bash -c is a fine escape hatch when the script is long, as long as you remember that variables from the outer shell need to be passed in as arguments or exported.
Checking an empty result
The shell fixes are easy once you know them, and you can put them into the agent's instructions (quote everything, use arrays for lists, ${var} before a colon, run long scripts with bash -c). The part that I think matters more is how we read an empty result. "No diff", "no matches" and "no commits" are answers only if the same command, on the same machine, could have returned something. So before trusting an empty result for a decision, it's worth running a positive control next to it:
- run the same
git diffagainst a commit you know touches those files, and check that it shows something; - check the same question from a second direction, for example with
git rev-parse <ref>:<path>on each branch, which prints the blob id of a file, so two branches with the same content print the same id; - for a grep that finds nothing, grep for something you know is there with the same flags.
None of these is clever, they're the shell version of checking that a test can fail before trusting that it passes.
An agent's "nothing found" is only as good as the shell that ran it, which is easy to forget when the output looks clean. The zsh fixes belong in the agent's instructions, where they stop most of these before they happen, but the habit that would have caught each of the silent ones is running a check that can come back non-empty next to any empty result a decision depends on, instead of finding out later because some other check happened to disagree.