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

# 运行 Shell 命令

Bun Shell 是一个内置于 Bun 的跨平台类 bash shell。

它从 JavaScript 和 TypeScript 运行 shell 命令。要开始使用，请从 `bun` 包中导入 `$` 函数。

```ts foo.ts icon="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/icons/typescript.svg?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=787d39d6a7d96d7f9540dc74344eba23" theme={null}
import { $ } from "bun";

await $`echo Hello, world!`; // => "Hello, world!"
```

***

`$` 函数是一个标记模板字面量，它运行命令并返回一个 Promise，该 Promise 在命令输出完成后解析。

```ts foo.ts icon="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/icons/typescript.svg?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=787d39d6a7d96d7f9540dc74344eba23" theme={null}
import { $ } from "bun";

const output = await $`ls -l`.text();
console.log(output);
```

***

要遍历输出的每一行，请使用 `lines` 方法。

```ts foo.ts icon="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/icons/typescript.svg?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=787d39d6a7d96d7f9540dc74344eba23" theme={null}
import { $ } from "bun";

for await (const line of $`ls -l`.lines()) {
  console.log(line);
}
```

***

参见 [Bun Shell](/runtime/shell)。
