194

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

4 Answers 4

181
VAR1=value1 VAR2=value2 myScript args ...
7
  • 3
    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. Commented Oct 26, 2016 at 15:58
  • 13
    with scripts it works but how about VAR1="hello" echo $VAR1 does not return anything?
    – Zibri
    Commented Aug 7, 2019 at 7:53
  • 7
    @Zibri it is about when the expansion is happening. Probably you can do something like that: VAR1="hello" bash -c 'echo $VAR1'
    – cybergrind
    Commented Oct 1, 2019 at 6:37
  • Upvoted for showing that this is possible even for multiple environment variables.
    – Binarus
    Commented Jan 22, 2020 at 22:08
  • Can we do this with export so the variable remains with subprocesses too?
    – jozxyqk
    Commented Jul 1, 2021 at 2:27
83
env VAR=value myScript args ...
10
  • 25
    Or VAR=value myScript args ...
    – Rockallite
    Commented Jan 26, 2014 at 6:25
  • 14
    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?
    – qubodup
    Commented Mar 23, 2015 at 1:27
  • 28
    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 Commented Mar 23, 2015 at 10:19
  • 21
    What is the difference between using env and not using it? Commented Jan 1, 2018 at 23:18
  • 2
    @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.
    – elquimista
    Commented Nov 19, 2019 at 21:15
45

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
  • 16
    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. Commented Aug 20, 2011 at 0:27
  • 2
    @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). Commented Aug 20, 2011 at 0:29
  • 5
    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. Commented Aug 20, 2011 at 18:10
  • 10
    @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. Commented Aug 20, 2011 at 18:47
  • 4
    @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. Commented Aug 20, 2011 at 20:14
0

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
  • Your answer is unclear and lacks supporting information. Commented Jan 17 at 3:22
  • @TonyWilliams is that clear to you?
    – zhao Wang
    Commented Jan 20 at 14:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.