iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐕

[Bash] Using Standard Input with Node.js, Deno, and Bun One-liners

に公開
2

To receive output from other Linux commands, you need to use standard input.

First, Node.js. You need to specify --input-type=module to use import statements (Node.js v22)

shell
echo -n Hello World | node --input-type=module -e "import { createInterface } from 'node:readline'; for await (const line of createInterface({ input: process.stdin })) { console.log(line); }"
eval
import { createInterface } from 'node:readline';

for await (const line of createInterface({ input: process.stdin })) { 
  console.log(line);
}

Next is Deno

shell
echo -n "Hello World" | deno eval "for await (const chunk of Deno.stdin.readable) { console.log(new TextDecoder().decode(chunk)); }"

Use Deno.stdin.readable

eval
import { createInterface } from 'node:readline';

for await (const line of createInterface({ input: process.stdin })) { 
  console.log(line);
}

Finally, Bun

shell
echo -n Hello World | bun -e "for await (const chunk of Bun.stdin.stream()) { console.log(Buffer.from(chunk).toString()); }"
eval
for await (const chunk of Bun.stdin.stream()) {
  console.log(Buffer.from(chunk).toString())
}

Discussion