如何在 Linux 上运行命令前临时清空 Bash 环境变量


我是个 bash shell 用户。我想临时清空 bash shell 环境变量。但我不想删除或者 unset 一个输出的环境变量。我怎样才能在 bash 或 ksh shell 的临时环境中运行程序呢?

你可以在 Linux 或类 Unix 系统中使用 env 命令设置并打印环境。env 命令可以按命令行指定的变量来修改环境,之后再执行程序。

 

如何显示当前环境?

打开终端应用程序并输入下面的其中一个命令:

  1. printenv

  1. env

输出样例:

Fig.01: Unix/Linux: 列出所有环境变量

Fig.01: Unix/Linux: 列出所有环境变量

 

统计环境变量数目

输入下面的命令:

  1. env | wc -l
  2. printenv | wc -l # 或者

输出样例:

  1. 20

 

在干净的 bash/ksh/zsh 环境中运行程序

语法如下所示:

  1. env -i your-program-name-here arg1 arg2 ...

例如,要在不使用 http_proxy 和/或任何其它环境变量的情况下运行 wget 程序。临时清除所有 bash/ksh/zsh 环境变量并运行 wget 程序:

  1. env -i /usr/local/bin/wget www.cyberciti.biz
  2. env -i wget www.cyberciti.biz # 或者

这当你想忽视任何已经设置的环境变量来运行命令时非常有用。我每天都会多次使用这个命令,以便忽视 http_proxy 和其它我设置的环境变量。

 

例子:使用 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]

 

例子:忽视 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 选项使 env 命令完全忽视它继承的环境。但是,它并不会阻止你的命令(例如 wget 或 curl)设置新的变量。同时,也要注意运行 bash/ksh shell 的副作用:

  1. env -i env | wc -l ## 空的 ##
  2. # 现在运行 bash ##
  3. env -i bash
  4. ## bash 设置了新的环境变量 ##
  5. env | wc -l

 

例子:设置一个环境变量

语法如下:

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

例如设置 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

作者:Vivek Gite 译者:ictlyh 校对:wxy

本文由 LCTT 原创翻译,Linux中国 荣誉推出

本文永久更新链接地址

相关内容