198

On Linux (Ubuntu 11.04 (Natty Narwhal)) 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 application that saves to HOME portable by temporarily setting HOME to a folder in the present working directory, and then launching the application.

2
  • 6
    It would be harder if you wanted the setting to last beyond the duration of the script Commented Aug 20, 2011 at 0:01
  • unix.stackexchange.com/questions/126938/… Commented Dec 16, 2021 at 1:27

4 Answers 4

184
VAR1=value1 VAR2=value2 myScript args ...
Sign up to request clarification or add additional context in comments.

7 Comments

I've done this myself numerous times to run vblank_mode=0 glxgears. It works, but it also says vblank_mode=0: command not found after running, whereas prepending env doesn't cause this. [testing...] Apparently zsh doesn't like it (yet still uses it correctly), but bash is fine with it. I guess I'll go with the env method from now on.
with scripts it works but how about VAR1="hello" echo $VAR1 does not return anything?
@Zibri it is about when the expansion is happening. Probably you can do something like that: VAR1="hello" bash -c 'echo $VAR1'
Upvoted for showing that this is possible even for multiple environment variables.
Can we do this with export so the variable remains with subprocesses too?
|
83
env VAR=value myScript args ...

10 Comments

Or VAR=value myScript args ...
1. How come PATH=$PATH:XYZ echo $PATH | grep XYZ doesn't have any output though? 2. What is the difference between using and not using env?
because the shell expands the PATH variable before executing the echo command. You need to delay that expansion. One way: PATH=$PATH:XYZ sh -c 'echo $PATH' | grep XYZ -- the single quotes are the key here
What is the difference between using env and not using it?
@MohammedNoureldin not a full answer, but in my own experience, depending on shell type, using without env gives an error and other shells don't give an error, so I guess it's mostly safer to always use with env.
|
46

WARNING --- READ THE COMMENTS.


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

8 Comments

That's misleading. export will pass the variable to subshells, but it doesn't control the parent shell. If you're writing a script that begins with "#!/bin/sh" or the like, ANY variable you set will disappear when the script exits.
@brightlancer, that is true but does not appear to contradict anything I wrote. (With the exception of the possibility that the script might start a background process, but I think that is beyond the OP's level of sophistication and would only confuse).
The export is unnecessary. Also, your answer only works if his script invokes an interpreter (#!/bin/sh or the like). If his "script" doesn't, then what you just told him will persist beyond the end of his script. That is why I said your answer was misleading - it might be correct, it might not, but it's definitely got a part that's unnecessary and confusing because it may cause someone to think "export" is the necessary element he was looking for.
@brightlancer: The export is necessary if the OP's script invokes sub-scripts that themselves depend on $HOME, and I dared not assume that was not the case. Furthermore, bash will spawn a subshell to run a script even if the script has no shebang line but is just a text file with the execute bit set. Try it -- variable assignments in the script are not visible in the shell you invoke it from. It's only if you explicitly source the script that it will be executed by the same shell that you type the command into.
@brightlancer: The export is necessary if he wants $HOME to be inherited by any commands executed from the script. And if he doesn't, and the setting of $HOME is only for the benefit of the script itself, then he'd probably be better off modifying the script so it refers to something other than $HOME.
|
1

enter image description here We can use command env to alter environments or just use a simple augment method as bash mannual recording

The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters. These assignment statements affect only the environment seen by that command.

From Bash mannual

2 Comments

Your answer is unclear and lacks supporting information.
@TonyWilliams is that clear to you?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.