简体   繁体   English

如何从导出环境变量的npm脚本运行bash脚本

[英]How to run bash script from npm script that exports environment variables

I have a package.json that has the following script: 我有一个包含以下脚本的package.json:

"scripts": {
  "config": ". ./setup.sh"
},

The setup.sh file prompts the user for an API token, setup.sh文件提示用户输入API令牌,

read -p "Enter API Authorization Token: " val
export API_AUTH_TOKEN=$val

and an environment via a PS3 menu. 以及通过PS3菜单的环境。 Ex: entering 1 should export DEFAULT_ENV=' http://localhost:8000 '. 例如:输入1应该导出DEFAULT_ENV =' http:// localhost:8000 '。

And when I run this setup.sh via the terminal(. ./setup.sh), it works great. 当我通过终端(./setup.sh)运行这个setup.sh时,效果很好。 It's only when I run "npm run config", where it doesn't actually export those values, although it acts like it did. 只有当我运行“npm run config”时,它才会实际导出这些值,尽管它的行为与它一样。 I'm under the impression this is related to this script being a process within the other process, and so, not affecting the global environment. 我认为这与此脚本是另一个流程的流程有关,因此不会影响全局环境。 How do I make it so that it does? 我如何做到这一点呢?

This is because export only works in child processes and itself. 这是因为导出仅适用于子进程及其自身。

You can edit your file, adding the line to see it: 您可以编辑文件,添加行以查看它:

read -p "Enter API Authorization Token: " val
export API_AUTH_TOKEN=$val
echo $API_AUTH_TOEKEN

In fact it never affects the parent process (like the shell window) 实际上它永远不会影响父进程(如shell窗口)

To affect the global, you should save the variable in files like .bashrc and source .bashrc to make it into effect. 要影响全局,您应该将变量保存在.bashrcsource .bashrc等文件中以使其生效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM