Fatmawati Achmad Zaenuri/ShutterstockFatmawati Achmad Zaenuri / Shutterstock

$PATH is one of the silent manipulators in the background of your Linux computer. It quietly affects your user experience, but there’s nothing shady about it. We’ll explain what it does, and how you can adjust it.

$PATH是Linux计算机后台的无声操纵器之一。 它会悄悄地影响您的用户体验,但是没有什么阴暗的地方。 我们将解释它的作用以及如何调整它。

什么是Linux上的$ PATH,它如何工作? (What Is $PATH on Linux, and How Does It Work?)

When you type a command in a terminal window and press Enter, you kick off quite a lot of activity before your command is even executed.

当在终端窗口中键入命令并按Enter时,您甚至在执行命令之前就开始了大量的活动。

Bash is the default shell on most Linux distributions. It interprets the line of text you entered and identifies the command names intermingled with the parameters, pipes, redirections, and whatever else is there. It then locates the executable binaries for those commands and launches them with the parameters you supplied.

Bash是大多数Linux发行版中的默认Shell。 它解释您输入的文本行,并标识与参数, 管道 , 重定向以及任何其他内容混合在一起的命令名称。 然后,找到这些命令的可执行二进制文件,并使用您提供的参数启动它们。

The first step the shell takes to locate the executable is identifying whether a binary is even involved. If the command you use is within the shell itself (a “shell builtin”) no further search is required.

Shell定位可执行文件所采取的第一步是确定是否包含二进制文件。 如果您使用的命令在外壳程序本身( “外壳程序内置” )中,则无需进一步搜索。

Shell builtins are the easiest to find because they’re integral to the shell. It’s like having them in a toolbelt—they’re always with you.

Shell内置程序最容易找到,因为它们是Shell不可或缺的。 就像将它们放在工具带中一样-它们始终与您同在。

If you need one of your other tools, though, you have to go rummage in the workshop to find it. Is it on your workbench or a wall hanger? That’s what the $PATH environment variable does. It holds a list of places the shell searches and the order in which they’ll be searched.

但是,如果您需要其他工具之一,则必须在车间中翻找才能找到它。 是放在工作台上还是挂在墙上? 这就是$PATH环境变量的作用。 它包含外壳搜索的位置列表以及搜索它们的顺序。

If you want to see whether a command is a shell builtin, an alias, a function, or a standalone binary mv /work/unfile, you can use the type command as shown below:

如果要查看命令是内置的Shell,别名,函数还是独立的二进制mv / work / unfile ,则可以使用type命令,如下所示:

type clear
type cd

This tells us that clear is a binary file, and the first one found in the path is located at /usr/bin. You might have more than one version of clear installed on your computer, but this is the one the shell will try to use.

这告诉我们clear是一个二进制文件,并且在路径中找到的第一个文件位于/usr/bin 。 您的计算机上可能安装了多个版本的clear ,但这是外壳程序将尝试使用的版本。

Unsurprisingly, cd is a shell builtin.

毫不奇怪, cd是内置的shell。

列出您的$ PATH (Listing Your $PATH)

It’s easy to see what’s in your path. Just type the following to use the echo command and print the value held in the $PATH variable:

很容易看到您的路径。 只需键入以下命令即可使用echo命令并打印 $PATH变量中保存的值 :

echo $PATH

The output is a list of colon (:) delimited file system locations. The shell searches from left to right through the path, checking each file system location for a matching executable to perform your command.

输出是结肠的列表( : )分隔的文件系统中的位置。 Shell通过路径从左到右搜索,检查每个文件系统位置是否有匹配的可执行文件来执行命令。

We can pick our way through the listing to see the file system locations that will be searched, and the order in which they will be searched:

我们可以通过清单选择方法,以查看将要搜索的文件系统位置以及搜索顺序:

  • /usr/local/sbin

    /usr/local/sbin

  • /usr/local/bin

    /usr/local/bin

  • /usr/sbin

    /usr/sbin

  • /usr/bin

    /usr/bin

  • /sbin

    /sbin

  • /bin

    /bin

  • /usr/games

    /usr/games

  • /usr/local/games

    /usr/local/games

  • /snap/bin

    /snap/bin

Something that might not be immediately obvious is the search doesn’t start in the current working directory. Rather, it works its way through the listed directories, and only the listed directories.

可能不会立即显而易见的是搜索不在当前工作目录中开始。 而是通过列出的目录(仅列出的目录)工作。

If the current working directory isn’t in your path, it won’t be searched. Also, if you have commands stored in directories that aren’t in the path, the shell won’t find them.

如果当前工作目录不在您的路径中,则不会搜索该目录。 另外,如果您将命令存储在不在路径中的目录中,则Shell将找不到它们。

To demonstrate this, we created a small program called rf. When executed, rf prints the name of the directory from which it was launched in the terminal window. It’s located in /usr/local/bin. We also have a newer version in the /dave/work directory.

为了证明这一点,我们创建了一个名为rf的小程序。 执行后, rf在终端窗口中打印从中启动目录的名称。 它位于/usr/local/bin 。 在/dave/work目录中,我们还有一个较新的版本。

We type the following  which command to show us which version of our program the shell will find and use:

我们键入以下which命令, 以向我们展示外壳程序将找到并使用哪个程序版本 :

which rf

The shell reports the version it found is the one in the directory that’s in the path.

外壳程序报告找到的版本是路径中目录中的版本。

We type the following to fire it up:

我们输入以下内容来启动它:

rf

Version 1.0 of rf runs and confirms our expectations were correct. The version found and executed is located in /usr/local/bin.

rf 1.0版运行并确认我们的期望是正确的。 找到并执行的版本位于/usr/local/bin

To run any other version of rf on this computer, we’ll have to use the path to the executable on the command line, as shown below:

要在此计算机上运行其他任何版本的rf ,我们必须在命令行上使用可执行文件的路径,如下所示:

./work/rf

Now that we’ve told the shell where to find the version of rf we want to run, it uses version 1.1. If we prefer this version, we can copy it into the /usr/local/bin directory and overwrite the old one.

既然我们已经告诉Shell在哪里可以找到我们要运行的rf版本,它就使用1.1版。 如果希望使用此版本,可以将其复制到/usr/local/bin目录中并覆盖旧版本。

Let’s say we’re developing a new version of rf. We’ll need to run it frequently as we develop and test it, but we don’t want to copy an unreleased development build into the live environment.

假设我们正在开发rf的新版本。 在开发和测试它时,我们需要经常运行它,但是我们不想将未发布的开发版本复制到实时环境中。

Or, perhaps we’ve downloaded a new version of rf and want to do some verification testing on it before we make it publicly available.

或者,也许我们已经下载了rf的新版本,并希望在对它进行公开之前对其进行一些验证测试。

If we add our work directory to the path, we make the shell find our version. And this change will only affect us—others will still use the version of rf in /usr/local/bin .

如果将工作目录添加到路径,则使外壳程序找到我们的版本。 而且此更改只会影响我们,其他人仍将使用/usr/local/binrf版本。

将目录添加到您的$ PATH (Adding a Directory to Your $PATH)

You can use the export command to add a directory to the $PATH. The directory is then included in the list of file system locations the shell searches. When the shell finds a matching executable, it stops searching, so you want to make sure it searches your directory first, before /usr/local/bin.

您可以使用export命令将目录添加到$PATH 。 该目录然后包含在外壳程序搜索的文件系统位置列表中。 当外壳程序找到匹配的可执行文件时,它将停止搜索,因此您需要确保先搜索目录,然后是/usr/local/bin

This is easy to do. For our example, we type the following to add our directory to the start of the path so it’s the first location searched:

这很容易做到。 对于我们的示例,我们键入以下命令将目录添加到路径的开头,因此它是搜索到的第一个位置:

export PATH=/home/dave/work:$PATH

This command sets $PATH to be equal to the directory we’re adding, /home/dave/work, and then the entire current path.

此命令将$PATH设置为等于我们要添加的目录/home/dave/work ,然后等于整个当前路径。

The first PATH has no dollar sign ($). We set the value for PATH. The final $PATH has a dollar sign because we’re referencing the contents stored in the PATH variable. Also, note the colon (:) between the new directory and the $PATH variable name.

第一个PATH没有美元符号( $ )。 我们为PATH设置值。 最后的$PATH具有美元符号,因为我们引用的是存储在PATH变量中的内容。 另外,还要注意冒号( :新目录和之间) $PATH变量名。

Let’s see what the path looks like now:

让我们看看现在的路径是什么样的:

echo $PATH

Our /home/dave/work directory is added to the start of the path. The colon we provided separates it the rest of the path.

我们的/home/dave/work目录已添加到路径的开头。 我们提供的冒号将路径的其余部分分隔开。

We type the following to verify our version of rf is the first one found:

我们键入以下内容以验证我们的rf版本是找到的第一个:

which rf

The proof in the pudding is running rf, as shown below:

布丁中的证明正在运行rf ,如下所示:

rf

The shell finds Version 1.1 and executes it from /home/dave/work.

Shell找到版本1.1并从/home/dave/work执行它。

To add our directory to the end of the path, we just move it to the end of the command, like so:

要将目录添加到路径的末尾,只需将其移动到命令的末尾,如下所示:

export PATH=$PATH:/home/dave/work

永久进行更改 (Making the Changes Permanent)

As Beth Brooke-Marciniak said, “Success is fine, but success is fleeting.” The moment you close the terminal window, any changes you’ve made to the $PATH are gone. To make them permanent, you have to put your export command in a configuration file.

正如Beth Brooke-Marciniak所说:“成功很好,但成功却是短暂的。” 关闭终端窗口后,对$PATH所做的任何更改都将消失。 要使它们永久存在,必须将export命令放在配置文件中。

When you put the export command in your .bashrc file, it sets the path each time you open a terminal window. Unlike SSH sessions, for which you have to log in, these are called “interactive” sessions.

export命令放在.bashrc文件中时,每次打开终端窗口时,它都会设置路径。 与必须登录的SSH会话不同,这些会话称为“交互式”会话。

In the past, you would put the export command in your .profile file to set the path for log in terminal sessions.

过去,您需要将export命令放在.profile文件中,以设置登录终端会话的路径。

However, we found that if we put the export command in either the .bashrc or .profile files, it correctly set the path for both interactive and log in terminal sessions. Your experience might be different. To handle all eventualities, we’ll show you how to do it in both files.

但是,我们发现,如果将export命令放在.bashrc.profile文件中,它将正确设置交互式会话和登录终端会话的路径。 您的经验可能有所不同。 为了处理所有可能发生的情况,我们将在两个文件中向您展示如何进行处理。

Use the following command in your /home directory to edit the .bashrc file:

/home目录中使用以下命令来编辑.bashrc文件:

gedit .bashrc

The gedit editor opens with the .bashrc file loaded.

gedit编辑器将打开,并加载.bashrc文件。

Scroll to the bottom of the file, and then add the following export command we used earlier:

滚动到文件的底部,然后添加以下我们先前使用的导出命令:

export PATH=/home/dave/work:$PATH

Save the file. Next, either close and reopen the terminal window or use the dot command to read the .bashrc file, as follows:

保存文件。 接下来,关闭并重新打开终端窗口,或使用dot命令读取.bashrc文件,如下所示:

. .bashrc

. .bashrc

Then, type the following echo command to check the path:

然后,键入以下echo命令以检查路径:

echo $PATH

This adds the /home/dave/work directory to the start of the path.

这会将/home/dave/work目录添加到路径的开头。

The process to add the command to the .profile file is the same. Type the following command:

将命令添加到.profile文件的过程是相同的。 键入以下命令:

gedit .profile

The gedit editor launches with the .profile file loaded.

gedit编辑器将在加载.profile文件后启动。

Add the export command to the bottom of the file, and then save it. Closing and opening a new terminal window is insufficient to force the .profile file to be reread. For the new settings to take effect, you must log out and back in or use the dot command as shown below:

export命令添加到文件的底部,然后将其保存。 关闭和打开新的终端窗口不足以强制重新读取.profile文件。 为了使新设置生效,您必须注销并重新登录或使用dot命令,如下所示:

. .profile

为所有人设定道路 (Setting the Path for Everyone)

To set the path for everyone who uses the system, you can edit the /etc/profile file.

要为使用系统的每个人设置路径,您可以编辑/etc/profile文件。

You’ll need to use sudo, as follows:

您将需要使用sudo ,如下所示:

sudo gedit /etc/profile

When the gedit editor launches, add the export command to the bottom of the file.

gedit编辑器启动时,将export命令添加到文件底部。

Save and close the file. The changes will take effect for others the next time they log in.

保存并关闭文件。 更改将在其他人下次登录时生效。

安全注意事项 (A Note on Security)

Make sure you don’t accidentally add a leading colon “:” to the path, as shown below.

确保不要在路径中意外添加冒号“ : ”,如下所示。

If you do, this will search the current directory first, which introduces a security risk. Say you downloaded an archive file and unzipped it into a directory. You look at the files and see another zipped file. You call unzip once more to extract that archive.

如果这样做,这将首先搜索当前目录,这会带来安全风险。 假设您下载了一个存档文件并将其解压缩到目录中。 您查看文件,然后看到另一个压缩文件。 您再次调用unzip解压缩该存档。

If the first archive contained an executable file called unzip that was a malicious executable, you’d accidentally fire up that one instead of the real unzip executable. This would happen because the shell would look in the current directory first.

如果第一个档案包含一个名为unzip的可执行文件,它是一个恶意可执行文件,则您可能会意外地启动该文件而不是真正的unzip可执行文件。 发生这种情况是因为外壳程序将首先在当前目录中查找。

So, always be careful when you type your export commands. Use echo $PATH to review them and make sure they are the way you want them to be.

因此,键入export命令时请务必小心。 使用echo $ PATH查看它们,并确保它们符合您的期望。

翻译自: https://www.howtogeek.com/658904/how-to-add-a-directory-to-your-path-in-linux/

如何在Linux中将目录添加到$ PATH相关推荐

  1. 在Windows中将目录添加到PATH环境变量

    本文翻译自:Adding directory to PATH Environment Variable in Windows I am trying to add C:\\xampp\\php to ...

  2. linux查将用户加入组,如何在Linux中将用户添加到组

    在本教程中,我们将介绍如何在Linux系统中将用户添加到组.我们还将向您展示如何从组中删除用户以及如何创建,删除和列出组. 先决条件 要向一个群组添加用户,您需要以具有sudo访问权限或root用户身 ...

  3. 如何在Linux中将用户添加到组

    Linux提供了通用的用户/组结构. 在本文中,我们将探讨如何创建用户并将其添加到组中. 注意:这些说明在使用Red Hat Enterprise Linux,Fedora和CentOS时有效. 它们 ...

  4. linux如何把用户加组内,在Linux中将用户添加到特定组的四种方法

    在Linux中将用户添加到特定组的四种方法 Linux组是用于管理Linux中用户帐户的组织单位. 对于Linux系统中的每个用户和组,它都有唯一的数字标识号. 它称为用户ID(UID)和组ID(GI ...

  5. linux 输出到文件 新,如何在Linux中将命令输出保存到文件

    原标题:如何在Linux中将命令输出保存到文件 在Linux中输出命令可以做很多事情. 您可以将命令的输出分配给变量,将其发送到另一个命令/程序以通过管道进行处理或将其重定向到文件以进行进一步分析. ...

  6. shell中encoding=utf-8_如何在Linux中将文件编码转换为UTF-8

    在这篇教程中,我们将解释字符编码的含义,然后给出一些使用命令行工具将使用某种字符编码的文件转化为另一种编码的例子.***,我们将一起看一看如何在 Linux 下将使用各种字符编码的文件转化为 UTF- ...

  7. 如何在Python中将元素添加到列表

    In this tutorial, we will learn different ways to add elements to a List in Python. 在本教程中,我们将学习使用Pyt ...

  8. Linux 中将用户添加到组的指令

    在 Linux 操作系统下,如何添加一个新用户到一个特定的组中?如何同时将用户添加到多个组中?又如何将一个已存在的用户移动到某个组或者给他增加一个组?对于不常用 Linux 的人来讲,记忆 Linux ...

  9. 如何在 Linux 系统中添加桌面图标

    在做嵌入式 Linux 产品的时候,我们通常会让机器自启动应用程序来处理相关业务,或者提供一个桌面系统让用户更加方便操作.那么这时候,我们就需要将编译好的应用程序添加到桌面,以便使用,就像 Windo ...

最新文章

  1. 澎思科技与新加坡国立大学等高校共研AI产品加快技术应用落地
  2. python操作系统-Python_操作系统的发展史
  3. The MIT License (MIT)
  4. 云原生网关开源、自研、商业化三位一体战略背后的思考
  5. linux apache mysql python 搭建_linux 下安装 mysql 并配置 python 开发环境
  6. hive 指定字段插入数据_Hive插入数据的几种常用方法
  7. 【运维】安装Ghost镜像系统步骤
  8. 分享几个特别好用且免费的图片/视频/gif/mp3压缩网站
  9. Linux 搭建 KMS 服务器
  10. java中null字符串与字符串长度为0的区别
  11. 传销三级的认定标准_应为传销案件中“劳务性工作人员”争取“不起诉”—传销犯罪辩护与研究(四十五)...
  12. 怎么还原计算机字体库,电脑字体怎么恢复默认设置
  13. progress进度条滚动动画
  14. How to change exchange rate in miro manually?
  15. mysql 的几种缓存,数据库缓存几种方式 -解道Jdon
  16. 14. 手机蓝牙遥控机器人制作
  17. 微信小程序轮播中的current_手把手教你美化微信小程序中的轮播效果
  18. EDA程序设计--计时器设计
  19. 输入学生成绩,输出学生成绩等级,学习成绩>=90且<=100分的同学用A表示,60-89分之间的用B表示,<60且>0分以下的用C表示。其余输入都有误!“
  20. 屏幕色温自动调节小助手

热门文章

  1. POLYCOM视频会议系统应用
  2. 利用OpenCV和Python实现一个指纹识别系统总目录
  3. Lenovo的实习日记
  4. 如何给iPhone(苹果手机)安装ipa文件
  5. 北大计算机普博申请,北京大学2018级博士研究生试行住宿申请制
  6. c语言将数组元素循环右移k位,把一个含有N个元素的数组循环右移K位
  7. java由浅入深学习css
  8. LCMapString/LCMapStringEx实现简体字、繁体字的转换。
  9. 110 道 Python 面试笔试题超强汇总
  10. 『PDF』⇌『DWG』