Solved

Need a way to clear out Linux env variables

Posted on 2011-09-07
8
711 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:cschene
  • 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:cschene
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
 
LVL 9

Expert Comment

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

Open in new window

may help too
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:cschene
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:cschene
ID: 36504561
increasing points
0
 

Author Closing Comment

by:cschene
ID: 36504566
Great solution, fast response.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bash script question (chmod) 10 54
gdb doesn't stop on breakpoint 2 48
nagios alerts 3 32
Debugging a shell script 3 13
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

911 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now