0

I have a node program called xapp that runs in console. I would like xapp create a environment variable that stay alive until user close the terminal (not only while the program is running).

I tried this so far:

let key = 'bacon';
let MYVAR = 'AWESOME_VAR';
shell.exec(`export ${MYVAR}=${key}`);
shell.exec(`echo \$${MYVAR}`);    

This doesn't print anything in console.

but even if 'bacon' is showed, what I really want is that the environment var stay alive after xapp finish... So I could execute this in terminal:

echo $AWESOME_VAR

and see him showing 'bacon'.

What I'm trying to do is to get xpat to communicate through multiple executions, but only while the same terminal session is active.

I tried something using process.env without success, since the environment variables is valid only in the same process.

alexpfx
  • 6,412
  • 12
  • 52
  • 88
  • I think you are looking for the [source](https://en.wikipedia.org/wiki/Source_(command)) command. Write your environment vars to a file, the next time around shell.exec source with your filename and they should be available in process.env. – Will Sep 20 '19 at 05:29
  • it's a good solution. thanks – alexpfx Sep 20 '19 at 10:33
  • it doesn't work cause shelljs does't recognize the source command. So I tried to put the source into a sh file and execute it via shell.exec and it doesn't work too. Even if it executes the script, the var is not changed. – alexpfx Sep 26 '19 at 18:38
  • nevermind.. i get it to work using dotenv extension and writing the var in a .env file. – alexpfx Sep 26 '19 at 20:21

0 Answers0