bash-classify
Classify bash commands by their side-effect risk level.
What it does
bash-classify parses bash expressions using tree-sitter, classifies each command against a database of 150+ known commands, and outputs a structured JSON verdict. Commands are classified along two axes: classification (READONLY, LOCAL_EFFECTS, EXTERNAL_EFFECTS, DANGEROUS, UNKNOWN) describing what kind of effects a command has, and risk (LOW, MEDIUM, HIGH) describing how worried you should be.
Designed primarily as a Claude Code hook to automatically allow low-risk commands while flagging risky ones for human review.
Installation
uv tool install bash-classify
# or
pip install bash-classify
To track a commit rather than a release — worth doing if you want to test a change before it ships — pin it directly:
bash-classify @ git+https://github.com/fprochazka/bash-classify@<commit>
The version is derived from the last tag, so a commit past it resolves to a pre-release such as 0.12.1.dev3+g8699357. An ordinary constraint still admits that: >=0.12,<0.13 contains it, because a specifier set only excludes pre-releases when its prereleases flag is explicitly false, and it is not by default. Writing the floor as >=0.12.1.dev0,<0.13 is worth doing anyway — it says out loud that the pinned thing is a pre-release, and it sets that flag — but it is a marker for whoever reads the file next, not something the resolver needs.
Quick start
$ echo 'kubectl get pods -n production' | bash-classify | jq '.classification'
"READONLY"
$ echo 'git push --force origin main' | bash-classify | jq '.classification'
"DANGEROUS"
$ echo 'cp file.txt /etc/config' | bash-classify | jq '.classification'
"DANGEROUS"
$ echo 'find . -name "*.pyc" -delete' | bash-classify | jq '.classification'
"DANGEROUS"
Matching command shapes
Classification answers "how risky is this?". A deny hook usually has a narrower question:
"does this expression run command shape X?" bash-classify match answers that one. It
parses the expression, walks every invocation at every depth, and reports which of the
shapes in a rules file were actually invoked — so a heredoc body, an echo string, a
# comment or a grep pattern that merely names the command does not count.
# blocked-commands.yaml
rules:
- name: mr-discussions-api
command: [glab, api]
any_arg_matches: 'merge_requests/[^/?]+/(discussions|notes)(/|\?|$)'
- name: mr-view-comments
command: [glab, mr, view]
any_option: [--comments, -c]
- name: mr-note
command: [glab, mr, note]
except: [[glab, mr, note, list]]
- name: python-script
command: [python3]
except_option: [-c]
$ echo 'sudo glab mr note 42 -m hi' | bash-classify match --rules blocked-commands.yaml
{
"matches": [
{
"rule": "mr-note",
"command": ["glab", "mr", "note"],
"argv": ["glab", "mr", "note", "42", "-m", "hi"],
"via": ["sudo"]
}
],
"parse_warnings": []
}
Within one rule every condition given must hold; rules are independent of each other, and one invocation can match several. command is a prefix match against the resolved command path, so /usr/bin/glab --repo x mr note still resolves to glab mr note. A command path — in command and in every except entry alike — holds the binary and its subcommands only. An option never resolves into one, so a path that contains one is rejected when the rules file loads, rather than loading clean and matching nothing.
any_option and except_option are mirrors of each other: any_option requires at least one of the listed options to be present, except_option requires that none of them is. Both look at the options actually present, with values stripped (--comments=true counts as --comments) and declared clusters expanded (-wc carries -c); tokens after -- are positionals and count as absent. An option the command database does not declare still counts, so except_option: [-c] separates python3 script.py from python3 -c '...'. any_arg_matches is a Python re.search over every argument token — a pattern written for grep -E needs \S rather than [^[:space:]]. via lists the enclosing wrappers, outermost first.