11

For example, suppose I have the following code:

Deno.run({cmd: ['echo', 'hello']})

How do I collect the output of that command which is hello ?

Wong Jia Hau
  • 2,639
  • 2
  • 18
  • 30

1 Answers1

12

Deno.run returns an instance of Deno.Process. Use the method .output() to get the buffered output. Don't forget to pass "piped" to stdout/stderr options if you want to read the contents.

const cmd = Deno.run({
  cmd: ["echo", "hello"], 
  stdout: "piped",
  stderr: "piped"
});

const output = await cmd.output() // "piped" must be set

cmd.close(); // Don't forget to close it

.output() returns a Promise which resolves to a Uint8Array so if you want the output as a UTF-8 string you need to use TextDecoder

const outStr = new TextDecoder().decode(output); // hello
Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98