As I know, newly running shell script inherits it's environment variables. Is there a way to block this? (running shell without variable inheriting)
2 Answers
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...
- 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– Eonil2010-09-01 14:24:23 +00:00Commented Sep 1, 2010 at 14:24
- 1To 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 runzshorfish(with or without-ldoesn't matter) it pollutes my environment with all kinds of variables from the shell I started it. Doing the trick withenv -csolves this problem for me. I doenv -i TERM=xterm-256color $(which fish).Paidhi– Paidhi2012-02-22 19:33:08 +00:00Commented Feb 22, 2012 at 19:33 - 2This is useful if you have a cron task which is failing because the environment is not properly configured and you need to test it; also a good way to test if your scripts make assumptions about environment settings. Thanks!razzed– razzed2013-08-22 04:21:47 +00:00Commented Aug 22, 2013 at 4:21
- 2Another possible place this could be useful is testing some periodic jobs (such as cron jobs) to ensure there are no environmental dependencies on the script. Same thing with commands that would be run in an "Exec" resource in a puppet manifest.TommyTheKid– TommyTheKid2014-12-16 05:58:14 +00:00Commented Dec 16, 2014 at 5:58
One possibility (although it looks rather ugly):
exec -c $SCRIPT will start $SCRIPT with an empty environment. (see man bash search for exec \[-cl\]).
- 1This worked almost equally with env -i, however, I choose to use env -i because exec stops executing rest of script.Eonil– Eonil2010-09-01 15:26:53 +00:00Commented Sep 1, 2010 at 15:26