Linux 如何仅在脚本运行期间设置环境变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7128542/
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 set an environment variable only for the duration of the script?
提问by suchipi
On Linux (Ubuntu 11.04) in bash, is it possible to temporarily set an environment variable that will only be different from the normal variable for the duration of the script? For example, in a shell script, making an app that saves to HOME portable by temporarily setting HOME to a folder in the present working directory, and then launching the app.
在 bash 的 Linux (Ubuntu 11.04) 上,是否可以临时设置一个环境变量,该变量仅在脚本运行期间与正常变量不同?例如,在 shell 脚本中,通过将 HOME 临时设置为当前工作目录中的文件夹,然后启动应用程序,使保存到 HOME 的应用程序可移植。
回答by hmakholm left over Monica
Just put
就放
export HOME=/blah/whatever
at the point in the script where you want the change to happen. Since each process has its own set of environment variables, this definition will automatically cease to have any significance when the script terminates (and with it the instance of bash that has a changed environment).
在脚本中您希望更改发生的位置。由于每个进程都有自己的一组环境变量,当脚本终止时,这个定义将自动不再具有任何意义(以及环境改变的 bash 实例)。
回答by glenn Hymanman
env VAR=value myScript args ...
回答by Rockallite
VAR1=value1 VAR2=value2 myScript args ...