zx icon indicating copy to clipboard operation
zx copied to clipboard

Setting env variable is not working for me using `within`

Open GuillaumedesPommareSAP opened this issue 2 years ago • 2 comments

Hello,

I am implementing some nice scripts using zx but one feature is not working at all in my case, setting an environment variable (a java classpath, in my case, which can be huge) and using it in further calls. Note: doing some else like cd("..") is working !

Expected Behavior

$ echo $FOO
bar

Actual Behavior

$ echo $FOO

Steps to Reproduce the Problem

I am running:

await within(async () => {
  $.env.FOO = 'bar';
  await $`echo $FOO`;
});

or

await within(async () => {
  process.env.FOO = 'bar';
  await $`echo $FOO`;
});

or even

await within(async () => {
  await $`export FOO=bar`;
  await $`echo $FOO`;
});

Specifications

  • Version: "zx": "7.2.3"
  • Platform: Windows 11 + WSL

I only can find information that it should be working, and I do not know how to debug further to understand what is happening.

GuillaumedesPommareSAP avatar Aug 02 '23 22:08 GuillaumedesPommareSAP

I had the same problem. 截屏2023-08-24 11 29 24

yiluoegit avatar Aug 24 '23 03:08 yiluoegit

It works according to specs, you just need to replace the whole $.env object like that:

$.env = { ...process.env, FOO: "BAR" };

koshic avatar Oct 15 '23 16:10 koshic