> ## Documentation Index
> Fetch the complete documentation index at: https://docs.anonalabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Run code

> Run commands, read exit codes, set the environment and the working directory.

`commands.run` runs a shell command (`/bin/bash -l -c`) in the sandbox and waits for it. It returns stdout, stderr and the exit code; a non-zero exit raises an exception you can catch.

```python run_code.py theme={null}
from anona import Sandbox
from e2b import CommandExitException

with Sandbox.create("base") as sbx:
    r = sbx.commands.run("echo $GREETING from $(pwd)", envs={"GREETING": "hi"}, cwd="/tmp")
    print(r.stdout.strip(), r.exit_code)

    sbx.files.write("/tmp/fib.sh", "a=0; b=1; for i in $(seq 10); do echo $a; t=$((a+b)); a=$b; b=$t; done\n")
    print(sbx.commands.run("bash /tmp/fib.sh | tail -1").stdout.strip())

    print(sbx.commands.run("whoami", user="root").stdout.strip())

    try:
        sbx.commands.run("exit 3")
    except CommandExitException as e:
        print("exit code", e.exit_code)
```

```typescript run_code.ts theme={null}
import { Sandbox } from "@anona/sdk";
import { CommandExitError } from "e2b";

const sbx = await Sandbox.create("base");
const r = await sbx.commands.run("echo $GREETING from $(pwd)", { envs: { GREETING: "hi" }, cwd: "/tmp" });
console.log(r.stdout.trim(), r.exitCode);

console.log((await sbx.commands.run("whoami", { user: "root" })).stdout.trim());

try {
  await sbx.commands.run("exit 3");
} catch (e) {
  if (e instanceof CommandExitError) console.log("exit code", e.exitCode);
  else throw e;
}
await sbx.kill();
```

```bash run_code.sh theme={null}
# anona-compute exec exits with the command's exit code
anona-compute exec "$SANDBOX_ID" -e GREETING=hi -w /tmp -- 'echo $GREETING from $(pwd)'
anona-compute exec "$SANDBOX_ID" -u root -- whoami
anona-compute exec "$SANDBOX_ID" -- 'exit 3' || echo "exit code $?"

# one-shot: create, run, delete (also on error or Ctrl-C)
anona-compute run -t base -- uname -a
```

| Option | Python | TypeScript | CLI |
| - | - | - | - |
| Environment | `envs={...}` | `envs: {...}` | `-e KEY=VALUE` |
| Working directory | `cwd=` | `cwd:` | `-w` |
| User | `user="root"` | `user: "root"` | `-u root` |
| Stop waiting after | `timeout=` (seconds, default 60, `0` = none) | `timeoutMs:` | `--cmd-timeout 5m` |

Sandbox-wide environment variables go on create: `Sandbox.create("base", envs={"API_TOKEN": "…"})` or `anona-compute create -e API_TOKEN=…`.

## Install packages

Every template has `sudo` for `user`. Install what you need at runtime:

```python install.py theme={null}
from anona import Sandbox

with Sandbox.create("base") as sbx:
    r = sbx.commands.run("sudo apt-get update -qq && sudo apt-get install -y -qq cowsay >/dev/null && /usr/games/cowsay moo", timeout=300)
    print(r.stdout)
```

For Python and Node work, start from the `python` or `node` template so the toolchain is already there. Pause the sandbox between runs to keep what you installed.

## JSON output from the CLI

`--json` prints one JSON object per line, which suits scripts:

```bash exec_json.sh theme={null}
anona-compute exec "$SANDBOX_ID" --json -- 'echo out; echo err >&2'
# {"type":"stdout","data":"out\n"}
# {"type":"stderr","data":"err\n"}
# {"type":"exit","exitCode":0}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.