Solved

Need a way to clear out Linux env variables

Posted on 2011-09-07
8
733 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article will explain how to establish a SSH connection to Ubuntu through the firewall and using a different port other then 22. I have set up a Ubuntu virtual machine in Virtualbox and I am running a Windows 7 workstation. From the Ubuntu vi…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

710 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