How to temporarily clear Bash environment variables before running commands on Linux

Source: Internet
Author: User

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:

  1. printenv

Or

  1. env

Output example:

Fig.01: Unix/Linux: list all environment variables

 

Count the number of Environment Variables

Enter the following command:

  1. env | wc -l
  2. Printenv | wc-l # Or

Output example:

  1. 20

 

Run the program in a clean bash/ksh/zsh Environment

The syntax is as follows:

  1. 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:

  1. env -i /usr/local/bin/wget www.cyberciti.biz
  2. 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
  1. $ wget www.cyberciti.biz
  2. --2015-08-0323:20:23-- http://www.cyberciti.biz/
  3. Connecting to 10.12.249.194:3128... connected.
  4. Proxy request sent, awaiting response...200 OK
  5. Length: unspecified [text/html]
  6. Saving to:'index.html'
  7. index.html [<=>]36.17K87.0KB/s in0.4s
  8. 2015-08-0323:20:24(87.0 KB/s)-'index.html' saved [37041]

 

Example: Ignore http_proxy
  1. $ env -i /usr/local/bin/wget www.cyberciti.biz
  2. --2015-08-0323:25:17-- http://www.cyberciti.biz/
  3. Resolving www.cyberciti.biz...74.86.144.194
  4. Connecting to www.cyberciti.biz|74.86.144.194|:80... connected.
  5. HTTP request sent, awaiting response...200 OK
  6. Length: unspecified [text/html]
  7. Saving to:'index.html.1'
  8. index.html.1[<=>]36.17K115KB/s in0.3s
  9. 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:

  1. Env-I env | wc-l # null ##
  2. # Run bash now ##
  3. env -i bash
  4. # Bash sets new environment variables ##
  5. env | wc -l

 

Example: Set an environment variable

Syntax:

  1. env var=value /path/to/command arg1 arg2 ...
  2. ##Or ##
  3. var=value /path/to/command arg1 arg2 ...

For example, set http_proxy:

  1. 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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.