Loading...
    • Developer Guide
    • API Reference
    • MCP
    • Resources
    • Release Notes
    Search...
    ⌘K

    First steps

    Intro to ClaudeQuickstart

    Models & pricing

    Models overviewChoosing a modelWhat's new in Claude 4.5Migrating to Claude 4.5Model deprecationsPricing

    Build with Claude

    Features overviewUsing the Messages APIContext windowsPrompting best practices

    Capabilities

    Prompt cachingContext editingExtended thinkingStreaming MessagesBatch processingCitationsMultilingual supportToken countingEmbeddingsVisionPDF supportFiles APISearch resultsGoogle Sheets add-on

    Tools

    OverviewHow to implement tool useToken-efficient tool useFine-grained tool streamingBash toolCode execution toolComputer use toolText editor toolWeb fetch toolWeb search toolMemory tool

    Agent Skills

    OverviewQuickstartBest practicesUsing Skills with the API

    Agent SDK

    OverviewTypeScript SDKPython SDK

    Guides

    Streaming InputHandling PermissionsSession ManagementHosting the Agent SDKModifying system promptsMCP in the SDKCustom ToolsSubagents in the SDKSlash Commands in the SDKAgent Skills in the SDKTracking Costs and UsageTodo ListsPlugins in the SDK

    MCP in the API

    MCP connectorRemote MCP servers

    Claude on 3rd-party platforms

    Amazon BedrockVertex AI

    Prompt engineering

    OverviewPrompt generatorUse prompt templatesPrompt improverBe clear and directUse examples (multishot prompting)Let Claude think (CoT)Use XML tagsGive Claude a role (system prompts)Prefill Claude's responseChain complex promptsLong context tipsExtended thinking tips

    Test & evaluate

    Define success criteriaDevelop test casesUsing the Evaluation ToolReducing latency

    Strengthen guardrails

    Reduce hallucinationsIncrease output consistencyMitigate jailbreaksStreaming refusalsReduce prompt leakKeep Claude in character

    Administration and monitoring

    Admin API overviewUsage and Cost APIClaude Code Analytics API
    Console
    Tools

    Bash tool

    The bash tool enables Claude to execute shell commands in a persistent bash session, allowing system operations, script execution, and command-line automation.

    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

    Model compatibility

    ModelTool Version
    Claude 4 models and Sonnet 3.7 (deprecated)bash_20250124

    Older tool versions are not guaranteed to be backwards-compatible with newer models. Always use the tool version that corresponds to your model version.

    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

    Python
    import anthropic
    
    client = anthropic.Anthropic()
    
    response = client.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=1024,
        tools=[
            {
                "type": "bash_20250124",
                "name": "bash"
            }
        ],
        messages=[
            {"role": "user", "content": "List all Python files in the current directory."}
        ]
    )
    Shell
    curl https://api.anthropic.com/v1/messages \
      -H "content-type: application/json" \
      -H "x-api-key: $ANTHROPIC_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -d '{
        "model": "claude-sonnet-4-5",
        "max_tokens": 1024,
        "tools": [
          {
            "type": "bash_20250124",
            "name": "bash"
          }
        ],
        "messages": [
          {
            "role": "user",
            "content": "List all Python files in the current directory."
          }
        ]
      }'

    How it works

    The bash tool maintains a persistent session:

    1. Claude determines what command to run
    2. You execute the command in a bash shell
    3. Return the output (stdout and stderr) to Claude
    4. Session state persists between commands (environment variables, working directory)

    Parameters

    ParameterRequiredDescription
    commandYes*The bash command to run
    restartNoSet to true to restart the bash session

    *Required unless using restart

    Example: Multi-step automation

    Claude can chain commands to complete complex tasks:

    # User request
    "Install the requests library and create a simple Python script that fetches a joke from an API, then run it."
    
    # Claude's tool uses:
    # 1. Install package
    {"command": "pip install requests"}
    
    # 2. Create script
    {"command": "cat > fetch_joke.py << 'EOF'\nimport requests\nresponse = requests.get('https://official-joke-api.appspot.com/random_joke')\njoke = response.json()\nprint(f\"Setup: {joke['setup']}\")\nprint(f\"Punchline: {joke['punchline']}\")\nEOF"}
    
    # 3. Run script
    {"command": "python fetch_joke.py"}

    The session maintains state between commands, so files created in step 2 are available in step 3.


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

      Set up a bash environment

      Create a persistent bash session that Claude can interact with:

      import subprocess
      import threading
      import queue
      
      class BashSession:
          def __init__(self):
              self.process = subprocess.Popen(
                  ['/bin/bash'],
                  stdin=subprocess.PIPE,
                  stdout=subprocess.PIPE,
                  stderr=subprocess.PIPE,
                  text=True,
                  bufsize=0
              )
              self.output_queue = queue.Queue()
              self.error_queue = queue.Queue()
              self._start_readers()
    2. 2

      Handle command execution

      Create a function to execute commands and capture output:

      def execute_command(self, command):
          # Send command to bash
          self.process.stdin.write(command + '\n')
          self.process.stdin.flush()
          
          # Capture output with timeout
          output = self._read_output(timeout=10)
          return output
    3. 3

      Process Claude's tool calls

      Extract and execute commands from Claude's responses:

      for content in response.content:
          if content.type == "tool_use" and content.name == "bash":
              if content.input.get("restart"):
                  bash_session.restart()
                  result = "Bash session restarted"
              else:
                  command = content.input.get("command")
                  result = bash_session.execute_command(command)
              
              # Return result to Claude
              tool_result = {
                  "type": "tool_result",
                  "tool_use_id": content.id,
                  "content": result
              }
    4. 4

      Implement safety measures

      Add validation and restrictions:

      def validate_command(command):
          # Block dangerous commands
          dangerous_patterns = ['rm -rf /', 'format', ':(){:|:&};:']
          for pattern in dangerous_patterns:
              if pattern in command:
                  return False, f"Command contains dangerous pattern: {pattern}"
          
          # Add more validation as needed
          return True, None

    Handle errors

    When implementing the bash tool, handle various error scenarios:

    Follow implementation best practices

    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

    See tool use pricing for complete pricing details.

    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

    Combining with other tools

    The bash tool is most powerful when combined with the text editor and other tools.

    Next steps

    Tool use overview

    Learn about tool use with Claude

    Text editor tool

    View and edit text files with Claude

    • Overview
    • Model compatibility
    • Use cases
    • Quick start
    • How it works
    • Parameters
    • Implement the bash tool
    • Handle errors
    • Follow implementation best practices
    • Security
    • Key recommendations
    • Pricing
    • Common patterns
    • Development workflows
    • File operations
    • System tasks
    • Limitations
    • Combining with other tools
    • Next steps
    © 2025 ANTHROPIC PBC

    Products

    • Claude
    • Claude Code
    • Max plan
    • Team plan
    • Enterprise plan
    • Download app
    • Pricing
    • Log in

    Features

    • Claude and Slack
    • Claude in Excel

    Models

    • Opus
    • Sonnet
    • Haiku

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Claude Developer Platform

    • Overview
    • Developer docs
    • Pricing
    • Amazon Bedrock
    • Google Cloud’s Vertex AI
    • Console login

    Learn

    • Blog
    • Catalog
    • Courses
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Help and security

    • Availability
    • Status
    • Support center

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy

    Products

    • Claude
    • Claude Code
    • Max plan
    • Team plan
    • Enterprise plan
    • Download app
    • Pricing
    • Log in

    Features

    • Claude and Slack
    • Claude in Excel

    Models

    • Opus
    • Sonnet
    • Haiku

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Claude Developer Platform

    • Overview
    • Developer docs
    • Pricing
    • Amazon Bedrock
    • Google Cloud’s Vertex AI
    • Console login

    Learn

    • Blog
    • Catalog
    • Courses
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Help and security

    • Availability
    • Status
    • Support center

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    © 2025 ANTHROPIC PBC