Skip to main content

Documentation Index

Fetch the complete documentation index at: https://bun.com/docs/llms.txt

Use this file to discover all available pages before exploring further.

For CLI tools, it’s often useful to read from stdin. In Bun, the console object is an AsyncIterable that yields lines from stdin.
https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35bindex.ts
const prompt = "Type something: ";
process.stdout.write(prompt);
for await (const line of console) {
  console.log(`You typed: ${line}`);
  process.stdout.write(prompt);
}

Running this file results in a never-ending interactive prompt that echoes whatever the user types.
terminal
bun run index.ts
Type something: hello
You typed: hello
Type something: hello again
You typed: hello again

Bun also exposes stdin as a BunFile via Bun.stdin. This is useful for incrementally reading large inputs that are piped into the bun process. There is no guarantee that the chunks will be split line-by-line.
https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35bstdin.ts
for await (const chunk of Bun.stdin.stream()) {
  // chunk is Uint8Array
  // this converts it to text (assumes ASCII encoding)
  const chunkText = Buffer.from(chunk).toString();
  console.log(`Chunk: ${chunkText}`);
}

This will print the input that is piped into the bun process.
terminal
echo "hello" | bun run stdin.ts
Chunk: hello

See Docs > API > Utils for more useful utilities.