How to clear all env variables

Viewed 1239

If I do:

$ env

It will print out all the environmental variables. Is there a way to delete all variables without looping through them individually in bash?

1 Answers

Yes, but be careful what you wish for:

$ unset $(env | awk -F= '{print $1}') && env
bash: env: No such file or directory

The 2nd call to env(1) fails because PATH has been deleted.

An alternative might be exec -c, to execute some command from a subshell with no environment. But a better route, if you're worried about too much stuff in the environment, is to look back and see where that excess stuff came from, and clear it up there.

Related