Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export env variable in node.js

I like my node.js so much, that I want to use it is my bash start up script ~/.bashrc, but I do not know how to export variable.

Currently I have to use this approach:

export PS1=`node ~/PS1.js`
export PS2=`node ~/PS2.js`
export PATH=`node ~/PATH.js`

instead I want .bashrc look have

#!/usr/local/bin/node
//do something, define functions
export_var('PS1', PS1())
export_var('PS2', PS2())
export_var('PATH', generatePATH())

process.env.PATH = something does not export, only sets for the currently executing process, which is node itself.

like image 679
exebook Avatar asked Oct 20 '14 11:10

exebook


1 Answers

One possible way is to use JS to print out the export statements, then in shell to use eval to evaluate it in the current shell.

e.g. test.js

#!/usr/bin/env node
console.log('export A=40; export B=10');

In the shell:

eval `./test.js`
echo $A
like image 151
Jianwu Chen Avatar answered Sep 25 '22 11:09

Jianwu Chen