LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices



Reply
 
Search this Thread
Old 09-20-2007, 03:54 PM   #1
Garnett
LQ Newbie
 
Registered: Sep 2007
Posts: 2

Rep: Reputation: 0
Thumbs up (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?

Last edited by Garnett; 09-21-2007 at 01:58 PM. Reason: Added (Solved) to title
 
Old 09-20-2007, 04:53 PM   #2
ChrisScott
Member
 
Registered: Nov 2006
Location: St Albans, England
Distribution: Fedora c3/5, Suse pro 10/openSuse 10.2, RHES, Zenwalk.....
Posts: 97

Rep: Reputation: 15
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!
 
Old 09-20-2007, 09:21 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,310

Rep: Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039Reputation: 2039
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.
 
Old 09-21-2007, 02:00 PM   #4
Garnett
LQ Newbie
 
Registered: Sep 2007
Posts: 2

Original Poster
Rep: Reputation: 0
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).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to set environment variables in bash profile praveenkorukonda Linux - Newbie 1 02-22-2007 12:40 AM
Environment variables for bash htarko Programming 1 09-13-2006 04:57 PM
Bash script environment variables mbjunior99 Suse/Novell 4 12-28-2005 01:40 AM
Setting environment variables from shell script theta Linux - General 5 09-02-2004 09:50 PM
Setting environment variables from a script... sylvain_gnu Linux - Newbie 5 04-20-2004 01:31 PM


All times are GMT -5. The time now is 04:37 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration