# Hook if Condition

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

**Field:** Artificial Intelligence  
**Category:** Claude Code  
**Also known as:** if field

## Think of it like

It is like a guard who only checks red bags.

## 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

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

## 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.

## Why it matters

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

## Common mistake

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

## Three principles

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

## Related terms

- [Hook Matcher](https://learn.benibix.com/term/ai/hook-matcher)
- [Permission Rule](https://learn.benibix.com/term/ai/permission-rule)
- [Compound Command](https://learn.benibix.com/term/ai/bash-compound-command)

## Told apart from

- [Hook Matcher vs Hook if Condition](https://learn.benibix.com/compare/hook-matcher-vs-hook-if-condition): People think the matcher can pick a command, such as git push. It only sees the tool name, so Bash matches every shell command. The if field reads the call's input, with one permission rule such as Bash(git push *). Use the matcher to pick the tool, and if to pick the calls inside it.

---

Source: https://learn.benibix.com/term/ai/hook-if-condition
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-16
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
