Skip to content

Hook: chained commands (cd dir && cmd) are never rewritten #112

@Rmohid

Description

@Rmohid

Problem

Claude Code frequently emits chained commands like:

cd /Users/me/code/project && git status
cd /some/dir && python3 -m pytest tests/ -v
cd /foo && git add . && git commit -m "msg"

The hook script sets FIRST_CMD="$CMD" and then matches against patterns like ^git[[:space:]]+status. Since the full command starts with cd, none of the patterns match, and the hook silently passes through.

rtk discover shows this is the dominant source of missed savings -- 271 git status, 65 tail, 81 grep, 105 ls calls missed in 30 days of usage (~84K tokens saveable).

Fix

Extract the last command segment after && or ; for pattern matching, then reassemble the full command after rewriting:

CHAIN_PREFIX=""
FINAL_CMD="$CMD"

if [[ "$CMD" == *" && "* ]]; then
  CHAIN_PREFIX="${CMD% && *} && "
  FINAL_CMD="${CMD##* && }"
elif [[ "$CMD" == *" ; "* ]]; then
  CHAIN_PREFIX="${CMD% ; *} ; "
  FINAL_CMD="${CMD##* ; }"
fi

# ... match/rewrite against FINAL_CMD ...

# Reassemble
if [ -n "$CHAIN_PREFIX" ]; then
  REWRITTEN="${CHAIN_PREFIX}${REWRITTEN}"
fi

This uses bash parameter expansion (no regex issues with && in =~).

Environment

  • rtk 0.15.3
  • macOS (Tahoe)
  • Claude Code CLI

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2-importantDevrait être fixé bientôtbugSomething isn't workingeffort-medium1-2 jours, quelques fichiers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions