Overview
The bash tool provides Claude with:- Persistent bash session that maintains state
- Ability to run any shell command
- Access to environment variables and working directory
- Command chaining and scripting capabilities
Tool versions
Model | Tool Version |
---|---|
Claude 4 models and Sonnet 3.7 | bash_20250124 |
Claude Sonnet 3.5 (deprecated) | bash_20241022 |
Claude Sonnet 3.5 (deprecated) requires the
computer-use-2024-10-22
beta header when using the bash tool.The bash tool is generally available in Claude 4 models and Sonnet 3.7.Use cases
- Development workflows: Run build commands, tests, and development tools
- System automation: Execute scripts, manage files, automate tasks
- Data processing: Process files, run analysis scripts, manage datasets
- Environment setup: Install packages, configure environments
Quick start
How it works
The bash tool maintains a persistent session:- Claude determines what command to run
- You execute the command in a bash shell
- Return the output (stdout and stderr) to Claude
- Session state persists between commands (environment variables, working directory)
Parameters
Parameter | Required | Description |
---|---|---|
command | Yes* | The bash command to run |
restart | No | Set to true to restart the bash session |
restart
Example usage
Example usage
Example: Multi-step automation
Claude can chain commands to complete complex tasks:Implement the bash tool
The bash tool is implemented as a schema-less tool. When using this tool, you don’t need to provide an input schema as with other tools; the schema is built into Claude’s model and can’t be modified.1
Set up a bash environment
Create a persistent bash session that Claude can interact with:
2
Handle command execution
Create a function to execute commands and capture output:
3
Process Claude's tool calls
Extract and execute commands from Claude’s responses:
4
Implement safety measures
Add validation and restrictions:
Handle errors
When implementing the bash tool, handle various error scenarios:Command execution timeout
Command execution timeout
If a command takes too long to execute:
Command not found
Command not found
If a command doesn’t exist:
Permission denied
Permission denied
If there are permission issues:
Follow implementation best practices
Use command timeouts
Use command timeouts
Implement timeouts to prevent hanging commands:
Maintain session state
Maintain session state
Keep the bash session persistent to maintain environment variables and working directory:
Handle large outputs
Handle large outputs
Truncate very large outputs to prevent token limit issues:
Log all commands
Log all commands
Keep an audit trail of executed commands:
Sanitize outputs
Sanitize outputs
Remove sensitive information from command outputs:
Security
The bash tool provides direct system access. Implement these essential safety measures:
- Running in isolated environments (Docker/VM)
- Implementing command filtering and allowlists
- Setting resource limits (CPU, memory, disk)
- Logging all executed commands
Key recommendations
- Use
ulimit
to set resource constraints - Filter dangerous commands (
sudo
,rm -rf
, etc.) - Run with minimal user permissions
- Monitor and log all command execution
Pricing
The bash tool adds 245 input tokens to your API calls. Additional tokens are consumed by:- Command outputs (stdout/stderr)
- Error messages
- Large file contents
Common patterns
Development workflows
- Running tests:
pytest && coverage report
- Building projects:
npm install && npm run build
- Git operations:
git status && git add . && git commit -m "message"
File operations
- Processing data:
wc -l *.csv && ls -lh *.csv
- Searching files:
find . -name "*.py" | xargs grep "pattern"
- Creating backups:
tar -czf backup.tar.gz ./data
System tasks
- Checking resources:
df -h && free -m
- Process management:
ps aux | grep python
- Environment setup:
export PATH=$PATH:/new/path && echo $PATH
Limitations
- No interactive commands: Cannot handle
vim
,less
, or password prompts - No GUI applications: Command-line only
- Session scope: Persists within conversation, lost between API calls
- Output limits: Large outputs may be truncated
- No streaming: Results returned after completion