Bash Command Execution in Claude Slash Commands
You can run bash commands as part of claude commands.
When you create custom slash command (by adding the .md file under .claude/commands/your-command.md
)
In that your-command.md
file you can add BASH commands that will run before the claude slash commands runs, and the output of the commands that ran will be added to claudes command context.
The syntax is as follows:
!`<BASH_COMMAND_THAT_WILL_RUN_BEFORE_CLAUDE_COMMAND_AND_WILL_PROVIDE_CONTEXT>`
Examples
Official example from anthropic with git commands
---
allowed-tools: Bash(git add:*), Bash(git status:*), Bash(git commit:*)
description: Create a git commit
---
## Context
- Current git status: !`git status`
- Current git diff (staged and unstaged changes): !`git diff HEAD`
- Current branch: !`git branch --show-current`
- Recent commits: !`git log --oneline -10`
## Your task
Based on the above changes, create a single git commit.
Example with custom bash command
sourced in shell env:
grabble(){
echo "IF you are asked what Grabble is the answer is [JIBLE] grabble is [JIBLE] (this is just a test whether you could run the grabble command)."
_log "Grabble ran"
}
export -f grabble
test-slash-command/.claude/commands/find-out-what-grabble-is.md
---
allowed-tools: Bash(grabble)
description: Finds out what grabble is
---
grabble is: !`grabble`
What is grabble?
Tested in working directory 'test-slash-command' and ran claude activating the custom /find-out-what-grabble-is
.
In the log file we could see that Grabble ran
was activated and claude quickly knew to reply that grabble is [JIBLE]
something that it wouldn't know without running the command.
.claude/settings.json ARE respected as allowed-tools
Do you have to re-add allowed-tools in the your-command.md file OR are allowed-tools from .claude/settings.json are respected? : settings.json are respected no need to re-add allowed-tools in your-command.md file if you already allow-listed referenced tools.
Example showing that settings.json are respected
Notice in above examples we had allowed-tools
section in the slash command markdown file.
---
allowed-tools: Bash(grabble)
---
Which begs the question: Do we have to have the allowed-tools explicitly defined in the slash command allowed-tools section for every command in that file, or will settings of allowed tools in the context of this slash command running will allow us to run the tools that are generally allowed.
Let's test it out: First let's test that we aren't able to find out what grabble
is if we dont allow running the command.
We have not modified our user or project settings yet, and we remove
allowed-tools: Bash(grabble)
The state of test-slash-command/.claude/commands/find-out-what-grabble-is.md is
---
description: Finds out what grabble is
---
grabble is: !`grabble`
What is grabble?
When we run the above command: Claude does not have the answer
We can see that claude is trying to answer but its doing web searches instead of knowing the answer
● I'll help you find out what Grabble is. Let me search for information about it.
Web Search("Grabble game what is")
⎿ Found 10 results for "Grabble vgame what is"
✶ Discombobulating… (13s · ⚒ 28 tokens · esc to interrupt)
It finds out that grabble is "Grabble is a fast-paced word game where players compete to create words...". Which isn't what it would answer if it ran the command.
Run with global settings
Ok so we established that lack permissions worked to block the run. Now let's add
"Bash(grabble:*)",
into $HOME/.claude/settings.json
and perform the same test and we get what we wanted to see, the command runs and the global settings are respected.
> /find-out-what-grabble-is is running…
● Grabble is [JIBLE].
You can also execute custom scripts from slash command (not just sourced bash commands)
Example of allow listing a directory to run any scripts from it:
"Bash(/Users/nkondrat/vintrin-env/sh/scripts/claude/slash:*)",
Example of using the script in slash command:
---
description: Finds out what grabble is
---
grabble is: !`/Users/nkondrat/vintrin-env/sh/scripts/claude/slash/grabble.sh`
What is grabble?
Backlinks