As I know, newly running shell script inherits it's environment variables. Is there a way to block this? (running shell without variable inheriting)

link|improve this question

70% accept rate
feedback

3 Answers

up vote 4 down vote accepted

It seems you can prefix your script with env -i which will clear the environment before running the script:

env -i sh test.sh

From man env:

-i, --ignore-environment
              start with an empty environment

Not sure why you would want to do this though...

link|improve this answer
Thanks. This worked! I need this for calling build script in IDE without influencing IDE's configuration environmental variables. But still far away to goal. This may help your curiousness! – Eonil Sep 1 '10 at 14:24
To add another use case: I am currently evaluating fish and zsh as an alternative to bash. But I'm not quite ready to promote the candidate shell as my login shell (as in chsh .... If I just open a terminal window and run zsh or fish (with or without -l doesn't matter) it pollutes my environment with all kinds of variables from the shell I started it. Doing the trick with env -c solves this problem for me. I do env -i TERM=xterm-256color $(which fish). – Paidhi Feb 22 at 19:33
feedback

One possibility (although it looks rather ugly):

exec -c $SCRIPT will start $SCRIPT with an empty environment. (see man bash search for exec \[-cl\]).

link|improve this answer
1  
This worked almost equally with env -i, however, I choose to use env -i because exec stops executing rest of script. – Eonil Sep 1 '10 at 15:26
feedback

As far as I know, the answer is "No". You may override environmental variables within the script itself. The value of the environment variable should switch back to it's original value once the script is done running. You probably need to run the script as a child process: $ this.script.sh & so as to avoid having to log out of your shell ever single time your run the script in order to reset your environment variables to their original value.

Edit:

From an earlier poster, it would seem that I am wrong about it not being possible.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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