Bun Shell 是一個內建於 Bun 的跨平台、類 bash 的 shell。
它提供了一種簡單的方式,可以在 JavaScript 和 TypeScript 中執行 shell 命令。若要開始使用,請從 bun
套件匯入 $
函式,並使用它來執行 shell 命令。
import { $ } from "bun";
await $`echo Hello, world!`; // => "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);
}
請參閱 文件 > API > Shell 以取得完整文件。