Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Software
User Name
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.


  Search this Thread
Old 09-20-2007, 02:54 PM   #1
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 12:58 PM. Reason: Added (Solved) to title
Old 09-20-2007, 03:53 PM   #2
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:

for VAR in `env | cut -d= -f1`; do
unset $VAR
The env man page says something about 'env -i' but I couldn't get that to work!
Old 09-20-2007, 08:21 PM   #3
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,406

Rep: Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396
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, 01:00 PM   #4
LQ Newbie
Registered: Sep 2007
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks all. Here's what I finally went with.
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).


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-21-2007 11:40 PM
Environment variables for bash htarko Programming 1 09-13-2006 03:57 PM
Bash script environment variables mbjunior99 SUSE / openSUSE 4 12-28-2005 12:40 AM
Setting environment variables from shell script theta Linux - General 5 09-02-2004 08:50 PM
Setting environment variables from a script... sylvain_gnu Linux - Newbie 5 04-20-2004 12:31 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:01 AM.

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