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上(ubuntu11.04),是否可以临时设置一个环境变量,该变量只在脚本的持续时间内与正常变量不同?例如,在一个shell脚本中,创建一个应用程序,该应用程序可以通过暂时将HOME放到当前工作目录中的文件夹,然后启动应用程序。
3 个解决方案
#1
59
VAR1=value1 VAR2=value2 myScript args ...
#2
51
env VAR=value myScript args ...
#3
18
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实例)。
#1
59
VAR1=value1 VAR2=value2 myScript args ...
#2
51
env VAR=value myScript args ...
#3
18
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实例)。