How to set environment variable only for script time?

On Linux (Ubuntu 11.04) in bash, is it possible to temporarily set an environment variable that will be different from a regular variable for the duration of the script? For example, in a shell script, creating an application that saves in HOME portable, temporarily installing HOME in a folder in the current working directory, and then launching the application.

+73
linux bash environment-variables
Aug 19 '11 at 23:50
source share
3 answers
VAR1=value1 VAR2=value2 myScript args ... 
+65
Jan 26 '14 at 6:26
source share
 env VAR=value myScript args ... 
+56
Aug 20 2018-11-21T00:
source share

Just put

 export HOME=/blah/whatever 

at the point in the script where you want this to happen. Since each process has its own set of environment variables, this definition automatically ceases to have any value when the script exits (and with it an instance of bash that has a modified environment).

+18
Aug 19 2018-11-11T00:
source share



All Articles