Bun Shell 是 Bun 內置的跨平台 bash 風格 shell。
它提供了一種在 JavaScript 和 TypeScript 中運行 shell 命令的簡單方法。要開始使用,請從 bun 包導入 $ 函數並使用它來運行 shell 命令。
ts
import { $ } from "bun";
await $`echo Hello, world!`; // => "Hello, world!"$ 函數是一個標記的模板字面量,它運行命令並返回一個 promise,該 promise 解析為命令的輸出。
ts
import { $ } from "bun";
const output = await $`ls -l`.text();
console.log(output);要將輸出的每一行作為數組獲取,請使用 lines 方法。
ts
import { $ } from "bun";
for await (const line of $`ls -l`.lines()) {
console.log(line);
}請參閱 文檔 > API > Shell 獲取完整文檔。