# Set environment variables (/guides/runtime/set-env)

<!-- agent-signals: reading_time_min: 1 · est_tokens: 345 · updated: 2026-07-28 -->
Related: [Delete directories](/guides/runtime/delete-directory.md), [Delete files](/guides/runtime/delete-file.md), [Import a HTML file as text](/guides/runtime/import-html.md), [Import a JSON file](/guides/runtime/import-json.md), [Import a JSON5 file](/guides/runtime/import-json5.md), [Import a TOML file](/guides/runtime/import-toml.md)

Access the current environment variables with `process.env` or `Bun.env`.

```ts icon="/icons/typescript.svg" title="index.ts"
Bun.env.API_TOKEN; // => "secret"
process.env.API_TOKEN; // => "secret"
```

***

Set these variables in a `.env` file.

Bun reads the following files automatically (listed in order of increasing precedence).

* `.env`
* `.env.production`, `.env.development`, `.env.test` (depending on value of `NODE_ENV`)
* `.env.local` (not loaded when `NODE_ENV=test`)

```ini icon="settings" title=".env"
FOO=hello
BAR=world
```

***

You can also set variables on the command line.

<CodeGroup>
  <CodeBlockTabs defaultValue="Linux/macOS" groupId="linux-macos+windows">
    <CodeBlockTabsList>
      <CodeBlockTabsTrigger value="Linux/macOS">
        Linux/macOS
      </CodeBlockTabsTrigger>

      <CodeBlockTabsTrigger value="Windows">
        Windows
      </CodeBlockTabsTrigger>
    </CodeBlockTabsList>

    <CodeBlockTab value="Linux/macOS">
      ```sh icon="terminal"  
      FOO=helloworld bun run dev
      ```
    </CodeBlockTab>

    <CodeBlockTab value="Windows">
      ```sh icon="windows"  
      # Using CMD
      set FOO=helloworld && bun run dev

      # Using PowerShell
      $env:FOO="helloworld"; bun run dev
      ```
    </CodeBlockTab>
  </CodeBlockTabs>
</CodeGroup>

***

See [Environment variables](/runtime/environment-variables).
