Linux系统入门学习:在Linux中修改环境变量PATH

提问: 当我试着运行一个程序时,它提示“command not found”。 但这个程序就在/usr/local/bin下。我该如何添加/usr/local/bin到我的PATH变量下,这样我就可以不用指定路径来运行这个命令了。

在Linux中,PATH环境变量保存了一系列的目录用于用户在输入的时候搜索命令。PATH变量的值由一系列的由分号分隔的绝对路径组成。每个用户都有特定的PATH环境变量(由系统级的PATH变量初始化)。

Linux系统入门学习:在Linux中修改环境变量PATH

要检查用户的环境变量,用户模式下运行下面的命令:

  1. <span class="pln">$ echo $PATH</span>
  2. <span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">lib64</span><span class="pun">/</span><span class="pln">qt</span><span class="pun">-</span><span class="lit">3.3</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">:</span><span class="str">/bin:/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">:</span><span class="str">/usr/</span><span class="pln">sbin</span><span class="pun">:</span><span class="str">/sbin:/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">xmodulo</span><span class="pun">/</span><span class="pln">bin</span>

或者运行:

  1. <span class="pln">$ env </span><span class="pun">|</span><span class="pln"> grep PATH</span>
  2. <span class="pln">PATH</span><span class="pun">=</span><span class="str">/usr/</span><span class="pln">lib64</span><span class="pun">/</span><span class="pln">qt</span><span class="pun">-</span><span class="lit">3.3</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">:</span><span class="str">/bin:/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">:</span><span class="str">/usr/</span><span class="pln">sbin</span><span class="pun">:</span><span class="str">/sbin:/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">xmodulo</span><span class="pun">/</span><span class="pln">bin</span>

如果你的命令不存在于上面任何一个目录内,shell就会抛出一个错误信息:“command not found”。

如果你想要添加一个另外的目录(比如:/usr/local/bin)到你的PATH变量中,你可以用下面这些命令。

 

为特定用户修改PATH环境变量

如果你只想在当前的登录会话中临时地添加一个新的目录(比如:/usr/local/bin)给用户的默认搜索路径,你只需要输入下面的命令。

  1. <span class="pln">$ PATH</span><span class="pun">=</span><span class="pln">$PATH</span><span class="pun">:</span><span class="str">/usr/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin</span>

检查PATH是否已经更新:

  1. <span class="pln">$ echo $PATH</span>
  2. <span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">lib64</span><span class="pun">/</span><span class="pln">qt</span><span class="pun">-</span><span class="lit">3.3</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">:</span><span class="str">/bin:/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">:</span><span class="str">/usr/</span><span class="pln">sbin</span><span class="pun">:</span><span class="str">/sbin:/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">xmodulo</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">:</span><span class="str">/usr/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin</span>

更新后的PATH会在当前的会话一直有效。然而,更改将在新的会话中失效。

如果你想要永久更改PATH变量,用编辑器打开~/.bashrc (或者 ~/.bash_profile),接着在最后添加下面这行。

  1. <span class="kwd">export</span><span class="pln"> PATH</span><span class="pun">=</span><span class="pln">$PATH</span><span class="pun">:</span><span class="str">/usr/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin</span>

接着运行下面这行永久激活更改:

  1. <span class="pln">$ source </span><span class="pun">~</span><span class="str">/.bashrc (或者 source ~/</span><span class="pun">.</span><span class="pln">bash_profile</span><span class="pun">)</span>

 

改变系统级的环境变量

如果你想要永久添加/usr/local/bin到系统级的PATH变量中,像下面这样编辑/etc/profile。

  1. <span class="pln">$ sudo vi </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">profile</span>

 

  1. <span class="kwd">export</span><span class="pln"> PATH</span><span class="pun">=</span><span class="pln">$PATH</span><span class="pun">:</span><span class="str">/usr/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin</span>

你重新登录后,更新的环境变量就会生效了。

via: http://ask.xmodulo.com/change-path-environment-variable-linux.html

作者:Dan Nanni 译者:geekpi 校对:wxy

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

来源:http://linux.cn/article-5478-1.html

相关推荐