santosh
santosh

Reputation: 501

Unset all environment variables in C-Shell

I want to unset all environment variables through C-shell script. With unsetenv * command I can do that with command line. How can we make it work in our script? I am hitting undefined identifier issue when I run through script.

Upvotes: 0

Views: 10173

Answers (1)

Martin Tournoij
Martin Tournoij

Reputation: 27852

From tcsh(1):

   unsetenv pattern
           Removes  all  environment  variables whose names match pattern.
           `unsetenv *' thus removes all environment variables; this is  a
           bad idea.  It is not an error for nothing to be unsetenved.

So using unsetenv * should work. It is possible you have a (possible old) (t)csh version that doesn't support this feature?

Either way, as the manual page says unsetting everything is probably a bad idea. You will want to keep various variables such as HOME, USER, and some others. A better way might be something like:

foreach e (`env`)
    set name = `echo "$e" | cut -d "=" -f 1`

    # Note: this list should be longer.
    if ( "$name" != "HOME" ) then
        unsetenv "$name"
    endif
end

Upvotes: 2

Related Questions