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

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

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

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

 

如何显示当前环境?

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

  1. <span class="pln">printenv</span>

  1. <span class="pln">env</span>

输出样例:

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

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

 

统计环境变量数目

输入下面的命令:

  1. <span class="pln">env </span><span class="pun">|</span><span class="pln"> wc </span><span class="pun">-</span><span class="pln">l</span>
  2. <span class="pln">printenv </span><span class="pun">|</span><span class="pln"> wc </span><span class="pun">-</span><span class="pln">l </span><span class="com"># 或者</span>

输出样例:

  1. <span class="lit">20</span>

 

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

语法如下所示:

  1. <span class="pln">env </span><span class="pun">-</span><span class="pln">i your</span><span class="pun">-</span><span class="pln">program</span><span class="pun">-</span><span class="pln">name</span><span class="pun">-</span><span class="pln">here arg1 arg2 </span><span class="pun">...</span>

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

  1. <span class="pln">env </span><span class="pun">-</span><span class="pln">i </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">wget www</span><span class="pun">.</span><span class="pln">cyberciti</span><span class="pun">.</span><span class="pln">biz</span>
  2. <span class="pln">env </span><span class="pun">-</span><span class="pln">i wget www</span><span class="pun">.</span><span class="pln">cyberciti</span><span class="pun">.</span><span class="pln">biz </span><span class="com"># 或者</span>

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

 

例子:使用 http_proxy

  1. <span class="pln">$ wget www</span><span class="pun">.</span><span class="pln">cyberciti</span><span class="pun">.</span><span class="pln">biz</span>
  2. <span class="pun">--</span><span class="lit">2015</span><span class="pun">-</span><span class="lit">08</span><span class="pun">-</span><span class="lit">03</span><span class="lit">23</span><span class="pun">:</span><span class="lit">20</span><span class="pun">:</span><span class="lit">23</span><span class="pun">--</span><span class="pln"> http</span><span class="pun">:</span><span class="com">//www.cyberciti.biz/</span>
  3. <span class="typ">Connecting</span><span class="pln"> to </span><span class="lit">10.12</span><span class="pun">.</span><span class="lit">249.194</span><span class="pun">:</span><span class="lit">3128.</span><span class="pun">..</span><span class="pln"> connected</span><span class="pun">.</span>
  4. <span class="typ">Proxy</span><span class="pln"> request sent</span><span class="pun">,</span><span class="pln"> awaiting response</span><span class="pun">...</span><span class="lit">200</span><span class="pln"> OK</span>
  5. <span class="typ">Length</span><span class="pun">:</span><span class="pln"> unspecified </span><span class="pun">[</span><span class="pln">text</span><span class="pun">/</span><span class="pln">html</span><span class="pun">]</span>
  6. <span class="typ">Saving</span><span class="pln"> to</span><span class="pun">:</span><span class="str">'index.html'</span>
  7. <span class="pln">index</span><span class="pun">.</span><span class="pln">html </span><span class="pun">[</span><span class="pun"><=></span><span class="pun">]</span><span class="lit">36.17K</span><span class="lit">87.0KB</span><span class="pun">/</span><span class="pln">s </span><span class="kwd">in</span><span class="lit">0.4s</span>
  8. <span class="lit">2015</span><span class="pun">-</span><span class="lit">08</span><span class="pun">-</span><span class="lit">03</span><span class="lit">23</span><span class="pun">:</span><span class="lit">20</span><span class="pun">:</span><span class="lit">24</span><span class="pun">(</span><span class="lit">87.0</span><span class="pln"> KB</span><span class="pun">/</span><span class="pln">s</span><span class="pun">)</span><span class="pun">-</span><span class="str">'index.html'</span><span class="pln"> saved </span><span class="pun">[</span><span class="lit">37041</span><span class="pun">]</span>

 

例子:忽视 http_proxy

  1. <span class="pln">$ env </span><span class="pun">-</span><span class="pln">i </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">wget www</span><span class="pun">.</span><span class="pln">cyberciti</span><span class="pun">.</span><span class="pln">biz</span>
  2. <span class="pun">--</span><span class="lit">2015</span><span class="pun">-</span><span class="lit">08</span><span class="pun">-</span><span class="lit">03</span><span class="lit">23</span><span class="pun">:</span><span class="lit">25</span><span class="pun">:</span><span class="lit">17</span><span class="pun">--</span><span class="pln"> http</span><span class="pun">:</span><span class="com">//www.cyberciti.biz/</span>
  3. <span class="typ">Resolving</span><span class="pln"> www</span><span class="pun">.</span><span class="pln">cyberciti</span><span class="pun">.</span><span class="pln">biz</span><span class="pun">...</span><span class="lit">74.86</span><span class="pun">.</span><span class="lit">144.194</span>
  4. <span class="typ">Connecting</span><span class="pln"> to www</span><span class="pun">.</span><span class="pln">cyberciti</span><span class="pun">.</span><span class="pln">biz</span><span class="pun">|</span><span class="lit">74.86</span><span class="pun">.</span><span class="lit">144.194</span><span class="pun">|:</span><span class="lit">80.</span><span class="pun">..</span><span class="pln"> connected</span><span class="pun">.</span>
  5. <span class="pln">HTTP request sent</span><span class="pun">,</span><span class="pln"> awaiting response</span><span class="pun">...</span><span class="lit">200</span><span class="pln"> OK</span>
  6. <span class="typ">Length</span><span class="pun">:</span><span class="pln"> unspecified </span><span class="pun">[</span><span class="pln">text</span><span class="pun">/</span><span class="pln">html</span><span class="pun">]</span>
  7. <span class="typ">Saving</span><span class="pln"> to</span><span class="pun">:</span><span class="str">'index.html.1'</span>
  8. <span class="pln">index</span><span class="pun">.</span><span class="pln">html</span><span class="pun">.</span><span class="lit">1</span><span class="pun">[</span><span class="pun"><=></span><span class="pun">]</span><span class="lit">36.17K</span><span class="lit">115KB</span><span class="pun">/</span><span class="pln">s </span><span class="kwd">in</span><span class="lit">0.3s</span>
  9. <span class="lit">2015</span><span class="pun">-</span><span class="lit">08</span><span class="pun">-</span><span class="lit">03</span><span class="lit">23</span><span class="pun">:</span><span class="lit">25</span><span class="pun">:</span><span class="lit">18</span><span class="pun">(</span><span class="lit">115</span><span class="pln"> KB</span><span class="pun">/</span><span class="pln">s</span><span class="pun">)</span><span class="pun">-</span><span class="str">'index.html.1'</span><span class="pln"> saved </span><span class="pun">[</span><span class="lit">37041</span><span class="pun">]</span>

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

  1. <span class="pln">env </span><span class="pun">-</span><span class="pln">i env </span><span class="pun">|</span><span class="pln"> wc </span><span class="pun">-</span><span class="pln">l </span><span class="com">## 空的 ##</span>
  2. <span class="com"># 现在运行 bash ##</span>
  3. <span class="pln">env </span><span class="pun">-</span><span class="pln">i bash</span>
  4. <span class="com">## bash 设置了新的环境变量 ##</span>
  5. <span class="pln">env </span><span class="pun">|</span><span class="pln"> wc </span><span class="pun">-</span><span class="pln">l</span>

 

例子:设置一个环境变量

语法如下:

  1. <span class="pln">env </span><span class="kwd">var</span><span class="pun">=</span><span class="pln">value </span><span class="pun">/</span><span class="pln">path</span><span class="pun">/</span><span class="pln">to</span><span class="pun">/</span><span class="pln">command arg1 arg2 </span><span class="pun">...</span>
  2. <span class="com">## 或 ## </span>
  3. <span class="kwd">var</span><span class="pun">=</span><span class="pln">value </span><span class="pun">/</span><span class="pln">path</span><span class="pun">/</span><span class="pln">to</span><span class="pun">/</span><span class="pln">command arg1 arg2 </span><span class="pun">...</span>

例如设置 http_proxy:

  1. <span class="pln">env http_proxy</span><span class="pun">=</span><span class="str">"http://USER:[email protected]:3128/"</span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">wget www</span><span class="pun">.</span><span class="pln">cyberciti</span><span class="pun">.</span><span class="pln">biz</span>

via: How To: Temporarily Clear Bash Environment Variables on a Linux and Unix-like System

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

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

相关推荐