Skip to content

Claude Code's grep and find wrappers need [[ ]], exec -a, typeset, and command in a pipeline #323

Open

Description

@abhisheksp

Problem

Claude Code 2.1.316 and later writes shell functions for grep, find, and rg into the shell it runs commands in. The functions run Claude Code's own binary under another name. Strands Shell cannot run them, because it does not support four forms that the functions use. Bash runs each form.

Command Bash Strands Shell (main, 9851f51)
[[ -n x ]] && echo yes yes [[: command not found
(exec -a foo echo hi) hi -a: command not found
typeset -f nosuch; echo $? 1 typeset: command not found, 127
echo hi | command grep -c hi 1 command: command not found

The last row is command in a stage of a pipeline. command NAME as a whole pipeline also ran the shell function NAME, so a wrapper such as grep() { command grep "$@"; } called itself until the stack overflowed. A fix for that part, with a differential case and command-check tests, is ready; it will link here.

What the functions look like

From the snapshot that Claude Code 2.1.328 writes (paths shortened):

function grep {
  local _cc_a
  for _cc_a in ${1+"$@"}; do
    case "$_cc_a" in -*-filter*|-*-config*|---*|-[Zz]*|--null) command grep ${1+"$@"}; return ;; esac
  done
  if [[ -x /path/to/claude ]]; then
    if [[ -n ${ZSH_VERSION:-} ]]; then
      ARGV0=ugrep /path/to/claude -G --ignore-files --hidden -I ${1+"$@"}
    else
      (exec -a ugrep /path/to/claude -G --ignore-files --hidden -I ${1+"$@"})
    fi
  else command grep ${1+"$@"}; fi
}
if ! hash rg 2>/dev/null && ! typeset -f rg >/dev/null 2>&1; then
  function rg { ... same shape, with exec -a rg ... }
fi

Also

A shell function costs a lot of stack in each call. With the default max_depth of 64, the differential suite's test thread (2 MiB stack, debug build) overflowed and aborted before the depth limit refused the recursion. An embedder that runs the shell on a thread with the default stack cannot rely on max_depth to stop deep recursion. #318 reduces the stack that each level uses.

Expected

  • [[ … ]] with the common tests (-n, -z, -x, -f, -d, ==, !=, &&, ||, !).
  • exec -a NAME program args, or a clear refusal of -a.
  • typeset as declare, at least typeset -f NAME.
  • command NAME in any stage of a pipeline.
  • A function-depth limit that stops recursion before the stack runs out (see fix: reduce stack usage during recursive shell execution #318).
  • command -p NAME and command -- NAME, which exit 127 today.

Activity

  1. added
    bug
    on Nov 19, 2026
  2. abhisheksp commented on Nov 19, 2026

    @abhisheksp
    ContributorAuthor

    #324 (command NAME skips the function NAME) and #325 ([[ ]]) together make Claude Code's grep and find wrappers work. I measured this in an embedder that runs the shell under a policy, with Claude Code 2.1.316 and 2.1.328: echo hi | grep -c hi prints 1, grep -rn and find -name find the file, and nothing recurses. When the embedder refuses access to the agent's own binary, the wrappers fall back to command grep and command find.

    Neither PR covers exec -a, typeset, or command in a later pipeline stage. Those wrappers need them only on paths that run the agent's own binary, so I leave them open here.

    Also seen: find . -type f -exec grep -l needle {} \; and ... -exec grep -l needle {} + print nothing where bash lists the file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    Fields

    Language

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions