> ## Documentation Index
> Fetch the complete documentation index at: https://e2b.manicule.dev/llms.txt
> Use this file to discover all available pages before exploring further.
> For every page in one file, fetch https://e2b.manicule.dev/llms-full.txt (large).
> To search and read these docs as tools, connect the MCP server at https://e2b.manicule.dev/api/mcp.
> Every agent surface: https://e2b.manicule.dev/.well-known/llms-manifest.json

# Execute commands in sandbox (/docs/cli/exec-command)

<!-- agent-signals: reading_time_min: 1 · est_tokens: 273 · updated: 2026-07-30 -->
Related: [Authentication in CLI](/docs/cli/auth.md), [Connect to sandbox](/docs/cli/connect-to-sandbox.md), [Create sandbox](/docs/cli/create-sandbox.md), [List sandboxes](/docs/cli/list-sandboxes.md), [Shutdown running sandboxes](/docs/cli/shutdown-sandboxes.md)

You can execute commands in a running sandbox.

<CodeGroup>
```bash title="Terminal" terminal
e2b sandbox exec <sandbox-id> <command>
```
</CodeGroup>

### Pipe command from stdin

You can pipe directly into the sandbox as well:

```bash
echo "foo" | e2b sandbox exec <sandbox-id> <command>
```

### Run in background

Use the `--background` flag to run a command in the background and return immediately. The command will print the process ID (PID) to stderr:

```bash
e2b sandbox exec --background <sandbox-id> "sleep 60 && echo done"
```

### Set working directory

Use the `--cwd` flag to specify the working directory for the command:

```bash
e2b sandbox exec --cwd /home/user <sandbox-id> ls
```

### Run as specific user

Use the `--user` flag to run the command as a specific user:

```bash
e2b sandbox exec --user root <sandbox-id> apt-get update
```

### Set environment variables

Use the `--env` flag to set environment variables. This flag can be repeated for multiple variables:

```bash
e2b sandbox exec --env NODE_ENV=production --env DEBUG=true <sandbox-id> node app.js
```
