function

bun.spawn

function spawn<In extends Writable = 'ignore', Out extends Readable = 'pipe', Err extends Readable = 'inherit'>(
options: SpawnOptions<In, Out, Err> & { cmd: string[] }
): Subprocess<In, Out, Err>;

Spawn a new process

function spawn<In extends Writable = 'ignore', Out extends Readable = 'pipe', Err extends Readable = 'inherit'>(
cmds: string[],
options?: SpawnOptions<In, Out, Err>
): Subprocess<In, Out, Err>;

Spawn a new process

const proc = Bun.spawn(["echo", "hello"]);
const text = await proc.stdout.text();
console.log(text); // "hello\n"

Internally, this uses posix_spawn(2)

@param cmds

The command to run

The first argument is resolved to an absolute executable path. It must be a file, not a directory.

If you explicitly set PATH in env, that PATH is used to resolve the executable instead of the default PATH.

To check if the command exists before running it, use Bun.which(bin).