Bun Shell 是內置於 Bun 的跨平臺類 bash shell。
它提供了一種在 JavaScript 和 TypeScript 中執行 Shell 命令的簡單方法。要開始使用,請從 bun 包匯入 $ 函式,並使用它來執行 Shell 命令。
import { $ } from "bun";
await $`echo Hello, world!`; // => "Hello, world!"
$ 函式是一個帶有標籤的模板字面量,它執行命令並返回一個解析為命令輸出的 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);
}
有關完整文件,請參閱 文件 > API > Shell。