# 概述
<subtitle>在沙箱中运行终端命令。</subtitle>

您可以使用 `commands.run()` 方法在沙箱内运行终端命令。

```python
from ucloud_sandbox import Sandbox

sandbox = Sandbox.create()
result = sandbox.commands.run('ls -l')
print(result)
```

## 基本用法示例

```python
from ucloud_sandbox import Sandbox

sandbox = Sandbox.create()

# 执行命令
result = sandbox.commands.run('ls -la /home/user')

# 解析结果
if result.exit_code == 0:
    print(f"Success:\n{result.stdout}")
else:
    print(f"Error (Exit {result.exit_code}):\n{result.stderr}")

sandbox.kill()
```

## 更多功能

- [流式输出](/docs/agent-sandbox/commands/streaming.md) - 实时流式输出命令执行过程中的 stdout 和 stderr
- [后台运行命令](/docs/agent-sandbox/commands/background.md) - 在后台运行长时间命令并稍后终止
