Skip to content

Bash Tool

Execute shell commands.

Quick Example

from mamba_agents.tools import run_bash

# Run a command
result = run_bash("ls -la")
print(result.stdout)

# With timeout
result = run_bash("long_command", timeout=60)

# Check result
if result.return_code == 0:
    print("Success!")
else:
    print(f"Error: {result.stderr}")

if result.timed_out:
    print("Command timed out")

BashResult

Field Type Description
stdout str Standard output
stderr str Standard error
return_code int Exit code
timed_out bool Did it timeout?

API Reference

run_bash

run_bash(
    command: str,
    working_dir: str = ".",
    timeout: int = 30,
    env: dict[str, str] | None = None,
) -> BashResult

Execute a shell command.

PARAMETER DESCRIPTION
command

The shell command to execute.

TYPE: str

working_dir

Working directory for the command.

TYPE: str DEFAULT: '.'

timeout

Timeout in seconds (default: 30).

TYPE: int DEFAULT: 30

env

Optional environment variables to add.

TYPE: dict[str, str] | None DEFAULT: None

RETURNS DESCRIPTION
BashResult

BashResult with stdout, stderr, and return code.

RAISES DESCRIPTION
TimeoutError

If the command times out.

Source code in src/mamba_agents/tools/bash.py
def run_bash(
    command: str,
    working_dir: str = ".",
    timeout: int = 30,
    env: dict[str, str] | None = None,
) -> BashResult:
    """Execute a shell command.

    Args:
        command: The shell command to execute.
        working_dir: Working directory for the command.
        timeout: Timeout in seconds (default: 30).
        env: Optional environment variables to add.

    Returns:
        BashResult with stdout, stderr, and return code.

    Raises:
        TimeoutError: If the command times out.
    """
    import os

    # Merge environment
    full_env = os.environ.copy()
    if env:
        full_env.update(env)

    try:
        result = subprocess.run(
            command,
            shell=True,
            cwd=working_dir,
            env=full_env,
            capture_output=True,
            text=True,
            timeout=timeout,
        )
        return BashResult(
            stdout=result.stdout,
            stderr=result.stderr,
            return_code=result.returncode,
        )
    except subprocess.TimeoutExpired as e:
        return BashResult(
            stdout=e.stdout or "" if isinstance(e.stdout, str) else (e.stdout or b"").decode(),
            stderr=e.stderr or "" if isinstance(e.stderr, str) else (e.stderr or b"").decode(),
            return_code=-1,
            timed_out=True,
        )

BashResult dataclass

BashResult(
    stdout: str,
    stderr: str,
    return_code: int,
    timed_out: bool = False,
)

Result of a bash command execution.

ATTRIBUTE DESCRIPTION
stdout

Standard output from the command.

TYPE: str

stderr

Standard error from the command.

TYPE: str

return_code

Exit code of the command.

TYPE: int

timed_out

Whether the command timed out.

TYPE: bool