[quickjs-devel] How to pass environment variables to subprocesses?

  • From: John Gardner <gardnerjohng@xxxxxxxxx>
  • To: quickjs-devel@xxxxxxxxxxxxx
  • Date: Tue, 17 Mar 2020 15:18:31 +1100

The `os.exec()` function doesn't seem to support setting environment
variables in a subprocess. That is, its options parameter needs an `env`
option so something like this is possible:

const out = os.pipe();
const pid = os.exec(["/bin/sh", "-c", "echo $FOO"], {
    block: false,
    usePath: true,
    stdout: out[1],
    env: {
        FOO: "Bar",
    },
});
os.close(out[1]);
const stream = std.fdopen(out[0], "r");
console.log(stream.readAsString()); // Outputs "Bar"

Other related posts: