How To: Temporarily Clear Bash Environment Variables on a Linux and Unix-like System
I‘m a bash shell user. I would like to temporarily clear bash shell environment variables. I do not want to delete or unset an exported environment variable. How do I run a program in a temporary environment in bash or ksh shell?
You can use the env command to set and print environment on a Linux or Unix-like systems. The env command executes utility after modifying the environment as specified[donotprint][/donotprint] on the command line.
How do I display my current environment?
Open the terminal application and type any one of the following command:
Counting your environment variables
Type the following command:
env | wc -l
printenv | wc -l
Run a program in a clean environment in bash/ksh/zsh
The syntax is as follows:
env-i your-program-name-here arg1 arg2 ...
env -i your-program-name-here arg1 arg2 …
For example, run the wget program without using http_proxy and/or all other variables i.e. temporarily clear all bash/ksh/zsh environment variables and run the wget program:
This is very useful when you want to run a command ignoring any environment variables you have set. I use this command many times everyday to ignore the http_proxy and other environment variable I have set.
Example: With the http_proxy
$ wget sxi.io
--2015-08-03 23:20:23-- http://sxi.io/
Connecting to 10.12.249.194:3128... connected.
Proxy request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: 'index.html'
index.html [ <=> ] 36.17K 87.0KB/s in 0.4s
2015-08-03 23:20:24 (87.0 KB/s) - 'index.html' saved 
The option -i causes env command to completely ignore the environment it inherits. However, it does not prevent your command (such as wget or curl) setting new variables. Also, note down the side effect of running bash/ksh shell:
env-ienv|wc-l## empty ### Now run bash ##env-ibash## New enviroment set by bash program ##env|wc-l
env -i env | wc -l ## empty ##
# Now run bash ##
env -i bash
## New enviroment set by bash program ##
env | wc -l
The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.