LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-08-2011, 07:00 PM   #1
sharky
Member
 
Registered: Oct 2002
Posts: 539

Rep: Reputation: 74
bash unset all environment variables


I had some scripts done in tcsh that would start off as follows;

Code:
#!/bin/tcsh -f

# get USER, HOME and DISPLAY and then completely clear environment
set U = $USER
set H = $HOME
set D = $DISPLAY
unsetenv *

# set USER, HOME and DISPLAY and set minimal path.
setenv USER $U
setenv HOME $H
setenv DISPLAY $D

# initial path
set path = (/bin \
/usr/bin \
/usr/local/bin \
/usr/bin/X11 \
/usr/contrib/bin \
/usr/contrib/bin/X11)
This was motivated by an attempt to ensure that my scripts would run identically for all users.

The key was "unsetenv *" which would unset all environment variables without touching $U, $H, and $D which are not environment variables in this context.

What I need is the bash equivalent of "unsetenv *". Does such a thing exist?
 
Old 06-08-2011, 07:53 PM   #2
sharky
Member
 
Registered: Oct 2002
Posts: 539

Original Poster
Rep: Reputation: 74
This worked.

Code:
#!/bin/bash

# get USER, HOME and DISPLAY and then completely clear environment
U=$USER
H=$HOME
D=$DISPLAY

for i in $(env | awk -F"=" '{print $1}') ; do
unset $i ; done

# set USER, HOME and DISPLAY and set minimal path.
export USER=$U
export HOME=$H
export DISPLAY=$D

# initial path
export PATH=/usr/kerberos/bin:/bin:/usr/bin:/usr/bin/X11:/usr/local/bin
 
  


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
Environment variables with BASH petersk Red Hat 12 01-04-2011 10:46 PM
how to unset environment variable in bash suneel Linux - Newbie 7 09-14-2009 11:17 AM
Environment variables for bash htarko Programming 1 09-13-2006 04:57 PM
Bash script environment variables mbjunior99 SUSE / openSUSE 4 12-28-2005 01:40 AM
BASH variables getting unset outside of loop trevelluk Programming 2 03-25-2005 08:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:41 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration