Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set an environment variable only for the duration of the script?

People also ask

Is there a limit to environment variable length?

The theoretical maximum length of an environment variable is around 32,760 characters. However, you are unlikely to attain that theoretical maximum in practice. All environment variables must live together in a single environment block, which itself has a limit of 32767 characters.

How do you set a temporary environment variable?

Environment variables can be defined temporarily by booting to MS-DOS mode, defining variables with the SET command and then starting Windows. Variables defined in this way persist until the system is rebooted.


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).