LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   Ignoring Environment Variables when Starting a BASH Script (http://www.linuxquestions.org/questions/linux-software-2/ignoring-environment-variables-when-starting-a-bash-script-586154/)

Garnett 09-20-2007 03:54 PM

(Solved) Ignoring Environment Variables when Starting a BASH Script
 
Hi all!

I'm doing some BASH script writing, and I'm trying to figure out a way to keep my script from inheriting any variables that the shell calling it had happened to EXPORT, working instead off of an environment that only includes the environment variables from the applicable config files (/etc/profile, ~/.bashrc, etc).

Is there any way to do this?

ChrisScott 09-20-2007 04:53 PM

It's a bit of a dirty hack... but you could use:

Code:

for VAR in `env | cut -d= -f1`; do
unset $VAR
done

The env man page says something about 'env -i' but I couldn't get that to work!

chrism01 09-20-2007 09:21 PM

That should work, but you'll have to re-invoke /etc/profile and .bashrc and .bash_profile.
In fact you'd have to parse those files to just get env var defs and nothing else. :(
If you really want control, decide which vars/vals you want and add those to the top of your script, overwriting previous defs.

Garnett 09-21-2007 02:00 PM

Thanks all. Here's what I finally went with.
Code:

env -i /bin/bash --login -i
This invokes a BASH shell in an empty environment, but the BASH shell comes up with the expectation that it will have to load all the standard environment variables (due to specifying "login & interactive" in its options). :D


All times are GMT -5. The time now is 08:23 PM.