Bash Tools (Python)
Execute bash commands using either local system execution or secure sandboxed environments with comprehensive command management capabilities.Overview
The Bash Tools provide a unified interface for executing bash commands across different environments:- Local Execution: Direct execution on the local system
- Sandbox Execution: Secure execution in isolated E2B containers
- Multi-Command Support: Execute multiple commands in sequence
- Real-time Streaming: Live output monitoring with callbacks
Quick Start
Local Execution
Sandbox Execution
Core Features
Single Command Execution
Multi-Command Execution
Real-time Output Streaming
Interactive Commands
Command Builders
Pre-built command generators for common operations:Security Features
Command Validation
Argument Escaping
API Reference
Context Creation
Tool Creation
Operator Classes
BaseBashOperator (Abstract)
LocalBashOperator
SandboxBashOperator
Data Models
CommandOptions
CommandResult
BashToolResponse
MultiCommandResponse
Utility Functions
CommandBuilders Reference
Examples
Development Workflow
File System Operations
Data Processing Pipeline
Error Handling
Advanced Usage
Using Operator Directly
Custom Tool Configuration
Installation
Basic Installation
With E2B Support
Environment Variables
When using sandbox execution, configure E2B:Related Documentation
- File System Tools - File operations
- Code Executor - Code execution
- Framework Adapters - LangChain & LlamaIndex integration
- MCP Integration - Model Context Protocol
- TypeScript Bash Tools - TypeScript version
Examples Repository
Find complete working examples at:- bash_tools.py - Comprehensive bash tools examples
- langchain_adapter.py - LangChain integration
- llamaindex_adapter.py - LlamaIndex integration
Security Considerations
- Command Validation: Always validate user-provided commands
- Argument Escaping: Use
escape_shell_arg()for user input - Sandbox Isolation: Use sandbox execution for untrusted code
- Timeout Limits: Set appropriate timeouts for all commands
- Environment Variables: Be careful with sensitive data in env vars
Performance Tips
- Reuse Operators: Create operator once and reuse for multiple commands
- Batch Commands: Use multi-command tool for related operations
- Stream Output: Use callbacks for large output to avoid memory issues
- Timeout Management: Set realistic timeouts based on expected execution time
- Sandbox Lifecycle: Reuse sandbox instances when possible