How to temporarily clear Bash environment variables before running commands on Linux
I am a bash shell user. I want to temporarily clear the bash shell environment variables. But I don't want to delete or unset an output environment variable. How can I run a program in the temporary environment of bash or ksh shell?
You can use the env command in Linux or Unix-like systems to set and print the environment. The env command can modify the environment according to the variables specified by the command line, and then execute the program.
How to display the current environment?
Open the terminal application and enter one of the following commands:
printenv
Or
env
Output example:
Fig.01: Unix/Linux: list all environment variables
Count the number of Environment Variables
Enter the following command:
env | wc -l
Printenv | wc-l # Or
Output example:
20
Run the program in a clean bash/ksh/zsh Environment
The syntax is as follows:
env -i your-program-name-here arg1 arg2 ...
For example, you need to run the wget program without using http_proxy and/or any other environment variables. Temporarily clear all bash/ksh/zsh environment variables and run the wget program:
env -i /usr/local/bin/wget www.cyberciti.biz
Env-I wget www. cyberciti. biz # Or
This is useful when you want to ignore any environment variables you have set to run commands. I use this command multiple times every day to ignore http_proxy and other environment variables I set.
Example: Use http_proxy
$ wget www.cyberciti.biz
--2015-08-0323:20:23-- http://www.cyberciti.biz/
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.17K87.0KB/s in0.4s
2015-08-0323:20:24(87.0 KB/s)-'index.html' saved [37041]
Example: Ignore http_proxy
$ env -i /usr/local/bin/wget www.cyberciti.biz
--2015-08-0323:25:17-- http://www.cyberciti.biz/
Resolving www.cyberciti.biz...74.86.144.194
Connecting to www.cyberciti.biz|74.86.144.194|:80... connected.
HTTP request sent, awaiting response...200 OK
Length: unspecified [text/html]
Saving to:'index.html.1'
index.html.1[<=>]36.17K115KB/s in0.3s
2015-08-0323:25:18(115 KB/s)-'index.html.1' saved [37041]
-I option makes the env command completely ignore the inherited environment. However, it does not prevent your command (such as wget or curl) from setting new variables. At the same time, pay attention to the side effects of running bash/ksh shell:
Env-I env | wc-l # null ##
# Run bash now ##
env -i bash
# Bash sets new environment variables ##
env | wc -l
Example: Set an environment variable
Syntax:
env var=value /path/to/command arg1 arg2 ...
##Or ##
var=value /path/to/command arg1 arg2 ...
For example, set http_proxy:
env http_proxy="http://USER:PASSWORD@server1.cyberciti.biz:3128/"/usr/local/bin/wget www.cyberciti.biz
Via: How To: Temporarily Clear Bash Environment Variables on a Linux and Unix-like System
Author: Vivek Gite Translator: ictlyh Proofreader: wxy
This article was originally translated by LCTT and launched with the Linux honor in China
This article permanently updates the link address: