bash 如何在 node.js 中导出环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26464103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to export env variable in node.js
提问by exebook
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.
我非常喜欢我node.js
的,我想用它作为我的bash
启动脚本~/.bashrc
,但我不知道如何导出变量。
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
相反,我想.bashrc
看看有
#!/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.
process.env.PATH = something
不导出,只为当前正在执行的进程设置,也就是节点本身。
采纳答案by delixfe
Node.js will run in an separate process which gets a copy of the environment. You cannot change the environment of you parent process (the one executing .bashrc).
Node.js 将在一个单独的进程中运行,该进程获取环境的副本。您无法更改父进程(执行 .bashrc 的进程)的环境。
But the following question has an answer for you: Can a shell script set environment variables of the calling shell?
但是以下问题可以为您提供答案: shell 脚本可以设置调用 shell 的环境变量吗?
You can write a new script file from within node.js and call it via source
.
您可以从 node.js 中编写一个新的脚本文件并通过source
.