# Command Execution
<subtitle>Remotely execute Shell commands in sandboxes and get execution results.</subtitle>

>
> **Prerequisites**: Please complete [API Key Configuration](/docs/agent-sandbox/product/01-prerequisites) first

`commands.run()` is the most direct way to interact with sandboxes. You can execute any legitimate command in the sandbox just like operating a local terminal.

## Basic Usage

Calling the `run` method will block the current process until the command execution ends, and return a result object containing standard output (stdout), standard error (stderr), and exit code.

```python
from ucloud_sandbox import Sandbox

# 1. Create sandbox
sandbox = Sandbox.create()

# 2. Execute simple command
result = sandbox.commands.run('ls -la /home/user')

# 3. Parse result
if result.exit_code == 0:
    print(f"Success:\n{result.stdout}")
else:
    print(f"Error (Exit {result.exit_code}):\n{result.stderr}")
```

## Notes

>
> **Default Working Directory**: Commands are executed under `/home/user` by default. If you need to change directories, you can use `cd` in the command or specify via the `working_dir` parameter.

>
> **Timeout Control**: Long-running commands may be limited by API connection timeout. For long tasks, it is recommended to refer to [Run Commands in Background](/docs/agent-sandbox/sdk/commands/03-run-commands-in-background).
