如何在Linux中将目录添加到$ PATH

A Linux laptop with lines of green text in a terminal window.
Fatmawati Achmad Zaenuri/Shutterstock Fatmawati 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
The "type clear" and "type cd" commands in a terminal window.

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 "echo $PATH" command in a terminal window.

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 "which rf" command in a terminal window.

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
The "rf" command in a terminal window.

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
The "./work/rf" command in a terminal window.

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
The "export PATH=/home/dave/work:$PATH" command in a terminal window.

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
The "echo $PATH" command in a terminal window.

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 "which rf" command in a terminal window.

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

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

rf
The "rf" command in a terminal window.

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 .bashrc" command in a terminal window.

The gedit editor opens with the .bashrc file loaded.

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

The gedit editor with the ".bashrc" file loaded.

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
The ". .bashrc" and "echo $PATH" commands in a terminal window.

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 .profile" command in a terminal window.

The gedit editor launches with the .profile file loaded.

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

The gedit editor with the ".profile" file loaded.

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命令添加到文件底部。

The gedit editor with the "/etc/profile" file loaded.

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.

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

The "echo $PATH" command with a colon highlighted in a terminal window.

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/

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值