Skip to content

Set environment variables for commands (envs)

What you’ll do

Provide environment variables for a command using the envs parameter, without relying on export across separate calls.

Prerequisites

  • AGB_API_KEY
  • A valid image_id that supports command execution (commonly agb-code-space-1)

Quickstart

Minimal runnable example: pass env vars to a command, print output, then clean up.

python
from agb import AGB
from agb.session_params import CreateSessionParams

agb = AGB()
create_result = agb.create(CreateSessionParams(image_id="agb-code-space-1"))
if not create_result.success:
    raise SystemExit(f"Session creation failed: {create_result.error_message}")

session = create_result.session
try:
    result = session.command.execute(
        "echo $GREETING $TARGET",
        envs={"GREETING": "hello", "TARGET": "world"},
    )
    if not result.success:
        raise RuntimeError(f"Command failed: {result.error_message}")
    print("Output:", result.output)
finally:
    agb.delete(session)
typescript
import { AGB, CreateSessionParams } from "agbcloud-sdk";

const agb = new AGB();
const createResult = await agb.create(
  new CreateSessionParams({ imageId: "agb-code-space-1" })
);
if (!createResult.success || !createResult.session) {
  throw new Error(`Session creation failed: ${createResult.errorMessage}`);
}

const session = createResult.session;
try {
  const result = await session.command.execute(
    "echo $GREETING $TARGET",
    undefined, undefined,
    { GREETING: "hello", TARGET: "world" },
  );
  if (!result.success) throw new Error(`Command failed: ${result.errorMessage}`);
  console.log("Output:", result.output);
} finally {
  await agb.delete(session);
}

Common tasks

Set a single variable

python
session.command.execute("echo $FOO", envs={"FOO": "bar"})
typescript
await session.command.execute("echo $FOO", undefined, undefined, { FOO: "bar" });

Pass multiple variables

python
session.command.execute(
    "python -c 'import os; print(os.environ[\"A\"], os.environ[\"B\"])'",
    envs={"A": "1", "B": "2"},
)
typescript
await session.command.execute(
  "python -c 'import os; print(os.environ[\"A\"], os.environ[\"B\"])'",
  undefined, undefined,
  { A: "1", B: "2" },
);

Legacy alternative: inline env vars (still works)

python
session.command.execute("FOO=bar echo $FOO")
typescript
await session.command.execute("FOO=bar echo $FOO");

Note: inline env vars can be fine for simple cases, but envs is easier to manage and less error-prone for complex values.

Best practices

  • Do not rely on export VAR=... persisting between calls; it won’t.
  • Use envs for values with spaces/special characters to avoid shell quoting issues.
  • Avoid logging secrets; keep sensitive values out of printed output and logs.

Troubleshooting

Environment variables are missing inside the command

  • Likely cause: you used export in a previous call and expected it to persist.
  • Fix: pass envs={...} on the call that needs those variables.

The command prints empty values

  • Likely cause: variable name mismatch, or the shell expands before setting the env in some patterns.
  • Fix: double-check variable names; prefer envs and avoid tricky quoting.