运行 Shell 命令
Bun Shell 是内置于 Bun 中的跨平台类 bash shell。
它可以从 JavaScript 和 TypeScript 中运行 shell 命令。要开始使用,请从 bun 包中导入 $ 函数。
import { $ } from "bun";
await $`echo Hello, world!`; // => "你好,世界!"
$ 函数是一个标签模板字面量,用于执行命令并返回一个 Promise,该 Promise 会解析为命令输出。
import { $ } from "bun";
const output = await $`ls -l`.text();
console.log(output);
要遍历输出中的每一行,请使用 lines 方法。
import { $ } from "bun";
for await (const line of $`ls -l`.lines()) {
console.log(line);
}
请参阅 Bun Shell。