bashshellenvironment-variables

How to clear all env variables


If I do:

env

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


Solution

  • 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.