node.jslinuxpowershellshellenvironment-variables

How can I unset all environment variables at once?


I am trying to unset environment variables in my code in a teardown block. Is there a command to unset all variables at once?

For example

I am setting the following environment variables:

export username=test
export password=password
export table=employees

I am using unset command to unset the variables:

unset username
unset password
unset table

Is there a command to unset all at once something like, unset all?


Solution

  • You definitely can unset all environment variables, using something like the following:

    unset $(env | cut -d= -f1)
    

    However, you probably don't want to do that, since that will also unset important variable like PATH which will make most things stop working.

    A better way to do this would be to run your tests in a subshell, so you can create as many environment variables as you want and then they'll be destroyed when you exit the subshell. You could also try to limit the unsetting to lowercase environment variables by inserting a suitable grep invocation between the env and cut, but of course that may be blunter than you want.