Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I am trying to unset all environment variables from within a script. The script runs fine but if I run env it still shows all the variables set.
If I run the command from CLI, it works and the variables are unset.

unset `env | awk -F= '/^\w/ {print $1}' | xargs`

Have any idea how to run this from a script?
Also, have any idea how to source /etc/profile from a script? This doesn't work either.

I need to set variables with same names but different paths, depending on the instances my users need.

Later edit:
ok, I ended up with this (rather not elegant, but whatever) solution:

. script 

which contains:

unset `env | awk -F= '/^\w/ {print $1}'|egrep -v "HOSTNAME|TERM|SHELL|HISTSIZE|SSH_CLIENT|SSH_TTY|USER|LS_COLORS|KDEDIR|MAIL|PATH|INPUTRC|PWD|LANG|HISTIGNORE|SSH_ASKPASS|TEST|SHLVL|HOME|LD_ASSUME_KERNEL|LOGNAME|SSH_CONNECTION|LESSOPEN|HISTTIMEFORMAT|G_BROKEN_FILENAMES|_"|xargs`
source env_variable_file

Thanks!

share|improve this question

3 Answers 3

up vote 1 down vote accepted

This is a standard answer, basically. You can't do that, because the script runs in a new child process. That process gets its own environment, and can't change that of the parent, either to set variables or to unset them.

You can run the script in the current environment using the . command. And you can source /etc/profile in the same way:

. /etc/profile

share|improve this answer
    
untrue - just declare the variables in the parent script first, then when you source the child it can modify them. See my example on how a script can call /etc/profile to reset HISTSIZE. –  troyengel Dec 29 '10 at 15:11
    
If you source the child script, yes. We are saying the same thing. –  mattdm Dec 29 '10 at 15:34
    
this one actually worked, ". script" –  w00t Dec 29 '10 at 15:54
    
Thanks for your help! –  w00t Dec 29 '10 at 16:06

I tried to run your script and it does not work for me.

I google a bit and found this:

unset $(/usr/bin/env | egrep '^(\w+)=(.*)$' | \
egrep -vw 'PWD|USER|LANG' | /usr/bin/cut -d= -f1);

This one actually works ;-)

For sourcing files

source /etc/profile

is the right way as you said.

If I modify your script like this

unset $(env | awk -F= '{print $1}' | xargs)

it also works.

I do not think if there is any difference running the command interactively vs from a script.

share|improve this answer
    
his unset 'env/awk' method worked just fine for me. bash 4.1.7. –  troyengel Dec 29 '10 at 15:13
    
may be an OS thing, RHE 4, bash 3.00.15. –  w00t Dec 29 '10 at 15:40
    
Thanks for your help! –  w00t Dec 29 '10 at 16:05

Use -v to the unset command to rip items out of the environment; note at the end of this test script how you may get errors because we've unset $PATH so it can't find programs like 'id' and such.

#!/bin/sh

LIST=`env | awk -F= '/^\w/ {print $1}' | xargs`

for item in $LIST; do
  echo "$item = ${!item}";
done

unset -v $LIST

for item in $LIST; do
  echo "$item = ${!item}";
done

HISTSIZE=5555
echo $HISTSIZE
source /etc/profile
echo $HISTSIZE
share|improve this answer
    
well, I can't do it like this because I must assume that $LIST already contains variables set up manually by the user, and I need to drop them in order the set up same ones with different values. But yes, I ended up using -v, as you also suggested. –  w00t Dec 29 '10 at 15:53
    
Thanks for your help! –  w00t Dec 29 '10 at 16:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.