exec (async)


Node.js

  1. const { exec } = require('child_process')
  2. exec(`echo 'hello world'`, (error, stdout, stderr) => {
  3. if (error) {
  4. console.error(err)
  5. }
  6. if (stderr) {
  7. console.error(stderr)
  8. }
  9. if (stdout) {
  10. console.log(stdout)
  11. }
  12. })

Output

  1. hello world

Go

  1. package main
  2. import (
  3. "os"
  4. "os/exec"
  5. )
  6. func main() {
  7. cmd := exec.Command("echo", "hello world")
  8. cmd.Stdout = os.Stdout
  9. cmd.Stderr = os.Stderr
  10. cmd.Run()
  11. }

Output

  1. hello world