Skip to content
BENIBIX Learn

Hook if Condition

AI⌨️ Claude CodeConfiguration

Also known as if field

A field that runs a hook only when the call matches one rule.

Think of it like

It is like a guard who only checks red bags.

Example

Lucy's hook only cares about git push. She adds an if rule for it, and the hook stops running on every other command.

Why it matters

It saves a script start on every call the hook would just ignore.

Definition

The if field on a hook holds one permission rule, such as Bash(git *), and the hook runs only when the tool call matches it. It narrows a hook further than the matcher, which only sees the tool name. For Bash it checks each part of a command, so Bash(git *) matches npm test && git push. It holds exactly one rule, with no and or or.

Example code

json
{
  "hooks": {
    "PreToolUse": [
      { "matcher": "Bash", "hooks": [{ "type": "command", "if": "Bash(git push *)", "command": "./check-branch.sh" }] }
    ]
  }
}
Run the check on git push only.

Common mistake

Writing two rules in one if field. It takes only one, so write a second handler.

Key takeaways

  1. 1
    One permission rule. Like Bash(git *).
  2. 2
    Finer than matcher. Looks at input.
  3. 3
    Checks each part. Of a Bash line.

Compare