Need a way to clear out Linux env variables

Posted on 2011-09-07
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++
Question by:Christopher Schene
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
LVL 38

Accepted Solution

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`
   unset $VAR

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

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?


Chris Schene
LVL 38

Expert Comment

ID: 36500737
> only clears the env variables for the current login session
Yes. it only clear the current shell session.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 36502538
in bourne shells,
env -i

Open in new window

may help too

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`
   unset $VAR

Before executing the loop


After executing the loop

LVL 38

Expert Comment

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.

Author Comment

by:Christopher Schene
ID: 36504561
increasing points

Author Closing Comment

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

Featured Post

Three Considerations for Containers

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read our article on Experts Exchange.

Question has a verified solution.

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

The purpose of this article is to show how we can create Linux Mint virtual machine using Oracle Virtual Box. To install Linux Mint we have to download the ISO file from its website i.e. Once you open the link you will see …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.
Suggested Courses

632 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