Solved

Need a way to clear out Linux env variables

Posted on 2011-09-07
8
727 Views
Last Modified: 2012-05-12
I would like to delete all the environment variables in my LINUX session and then set all the env variables to a set of values I choose.

250 points for help
500 for code that does what I need. Could be Shell script, perl, c++
0
Comment
Question by:Christopher Schene
  • 4
  • 3
8 Comments
 
LVL 38

Accepted Solution

by:
wesly_chen earned 500 total points
ID: 36500697
For csh/tcsh, just do
unsetenv  *


For sh/bash shell
for  VAR  in  `/usr/bin/env | egrep '^(\w+)=(.*)$' |  egrep -vw 'PWD|USER|LANG|_' | /usr/bin/cut -d= -f1`
do
   unset $VAR
done


or (one-liner)
unset `env | awk -F= '/^\w/ {print $1}' | egrep -v 'PWD|USER|LANG|_' | xargs`
0
 

Author Comment

by:Christopher Schene
ID: 36500725
Hi Wesly,

thanks for the fast response.

Just to clarify: This only clears the env variables for the current login session, correct?

Thanks,

Chris Schene
0
 
LVL 38

Expert Comment

by:wesly_chen
ID: 36500737
> only clears the env variables for the current login session
Yes. it only clear the current shell session.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 9

Expert Comment

by:svs
ID: 36502538
in bourne shells,
env -i

Open in new window

may help too
0
 

Author Comment

by:Christopher Schene
ID: 36504137
I tried the loop before and I have uploaded the before and after env values

Is this what you would expect?

for  VAR  in  `/usr/bin/env | egrep '^(\w+)=(.*)$' |  egrep -vw 'PWD|USER|LANG|_' | /usr/bin/cut -d= -f1`
do
   unset $VAR
done

Before executing the loop

 env-before-clear.txt

After executing the loop

env-after-clear.txt
0
 
LVL 38

Expert Comment

by:wesly_chen
ID: 36504298
The env-after looks good to me.
Any specific environment variable you don't want and it still exist?
Try manually run "unset <env variable name>" to see if you can unset or not.
0
 

Author Comment

by:Christopher Schene
ID: 36504561
increasing points
0
 

Author Closing Comment

by:Christopher Schene
ID: 36504566
Great solution, fast response.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
linux 13 77
what do I need to host my own web sites? 13 53
list of sudo access for date range 5 29
SSL/TLS - openssl troubleshooting 3 42
1. Introduction As many people are interested in Linux but not as many are interested or knowledgeable (enough) to install Linux on their system, here is a safe way to try out Linux on your existing (Windows) system. The idea is that you insta…
Utilizing an array to gracefully append to a list of EmailAddresses
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question