Skip to content
Advertisement

How to unset all environment variables at once?

I am trying to unset environment variables in my code in 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

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement