Solved

Need a way to clear out Linux env variables

Posted on 2011-09-07
8
703 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

You ever wonder how to backup Linux system files just like Windows System Restore?  Well you can use Timeshift in Linux to perform those similar action.  This tutorial will show you how to backup your system files and keep regular intervals. Note…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
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.:
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…

758 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

18 Experts available now in Live!

Get 1:1 Help Now