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

# 读取环境变量

当前的环境变量可以通过 `process.env` 访问。

```ts index.ts icon="https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/typescript.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=e7767043c9e885c34f2d6c8fe2a95217" theme={"theme":{"light":"github-light","dark":"dracula"}}
process.env.API_TOKEN; // => "secret"
```

***

Bun 也通过 `Bun.env` 暴露这些变量；它是 `process.env` 的别名。

```ts index.ts icon="https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/typescript.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=e7767043c9e885c34f2d6c8fe2a95217" theme={"theme":{"light":"github-light","dark":"dracula"}}
Bun.env.API_TOKEN; // => "secret"
```

***

要将所有当前设置的环境变量打印到命令行，运行 `bun --print process.env`。这对于调试很有用。

```sh terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
bun --print process.env
```

```txt theme={"theme":{"light":"github-light","dark":"dracula"}}
BAZ=stuff
FOOBAR=aaaaaa
<更多行>
```

***

更多关于在 Bun 中使用环境变量的信息，请参阅 [文档 > 运行时 > 环境变量](/runtime/environment-variables)。
