简体   繁体   English

如何仅在脚本的持续时间内设置环境变量?

[英]How to set an environment variable only for the duration of the script?

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? 在bash上的Linux(Ubuntu 11.04)上,是否可以临时设置一个环境变量,该变量在脚本持续时间内只与普通变量不同? 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. 例如,在shell脚本中,通过临时将HOME设置为当前工作目录中的文件夹,然后启动应用程序,使应用程序保存到HOME便携式。

VAR1=value1 VAR2=value2 myScript args ...
env VAR=value myScript args ...

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实例),此定义将自动停止具有任何意义。

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

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