如何在 Linux 上运行命令前临时清空 Bash 环境变量
我是个 bash shell 用户。我想临时清空 bash shell 环境变量。但我不想删除或者 unset 一个输出的环境变量。我怎样才能在 bash 或 ksh shell 的临时环境中运行程序呢?
你可以在 Linux 或类 Unix 系统中使用 env 命令设置并打印环境。env 命令可以按命令行指定的变量来修改环境,之后再执行程序。
如何显示当前环境?
打开终端应用程序并输入下面的其中一个命令:
<span class="pln">printenv</span>
或
<span class="pln">env</span>
输出样例:
Fig.01: Unix/Linux: 列出所有环境变量
统计环境变量数目
输入下面的命令:
<span class="pln">env </span><span class="pun">|</span><span class="pln"> wc </span><span class="pun">-</span><span class="pln">l</span>
<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>
输出样例:
<span class="lit">20</span>
在干净的 bash/ksh/zsh 环境中运行程序
语法如下所示:
<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 程序:
<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>
<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
<span class="pln">$ wget 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">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>
<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>
<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>
<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>
<span class="typ">Saving</span><span class="pln"> to</span><span class="pun">:</span><span class="str">'index.html'</span>
<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>
<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
<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>
<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>
<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>
<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>
<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>
<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>
<span class="typ">Saving</span><span class="pln"> to</span><span class="pun">:</span><span class="str">'index.html.1'</span>
<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>
<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 的副作用:
<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>
<span class="com"># 现在运行 bash ##</span>
<span class="pln">env </span><span class="pun">-</span><span class="pln">i bash</span>
<span class="com">## bash 设置了新的环境变量 ##</span>
<span class="pln">env </span><span class="pun">|</span><span class="pln"> wc </span><span class="pun">-</span><span class="pln">l</span>
例子:设置一个环境变量
语法如下:
<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>
<span class="com">## 或 ## </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>
例如设置 http_proxy:
<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
相关推荐
昭君出塞 2020-11-23
xiaoyuerp 2020-08-17
blankt 2020-08-15
Jieen 2020-06-18
IsanaYashiro 2020-05-28
lanyou00 2020-05-26
昭君出塞 2020-05-25
zhushixia 2020-05-20
pointfish 2020-05-19
Attend 2020-05-15
屿刃 2020-04-29
lfjjia 2020-05-07
xiaohouye 2020-05-06
playlinuxxx 2020-05-05
Sabrina 2020-05-03
libao 2020-09-16
zjc 2020-09-03
84226432 2020-08-19