Link to home
Start Free TrialLog in
Avatar of Christopher Schene
Christopher ScheneFlag for United States of America

asked on

Need a way to clear out Linux env variables

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++
ASKER CERTIFIED SOLUTION
Avatar of wesly_chen
wesly_chen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Christopher Schene

ASKER

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
> only clears the env variables for the current login session
Yes. it only clear the current shell session.
in bourne shells,
env -i

Open in new window

may help too
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
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.
increasing points
Great solution, fast response.