bash 如何将 $HOME/opt/git/bin 放入我的路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/550490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:57:55  来源:igfitidea点击:

How can I put $HOME/opt/git/bin to my PATH?

gitbashunixpath

提问by Léo Léopold Hertz ??

I tried to include the following unsuccessfully to my ~/.profile:

我试图将以下内容包含到我的 ~/.profile 中,但没有成功:

export PATH='$HOME/opt/git/bin'

It seems not to work because $git gives me nothing. I am trying to install Git.

它似乎不起作用,因为 $git 什么也没给我。我正在尝试安装 Git。

I have also tried commands here.

我也在这里尝试过命令。

回答by matpie

You'll want to be careful with that command. It will overrideyour $PATH.

您需要小心使用该命令。它将覆盖您的 $PATH。

You might need to put it in ~/.bash_profileand change it to this:

您可能需要将其放入并将其~/.bash_profile更改为:

export PATH="$HOME/opt/git/bin:$PATH"

回答by Jonathan Leffler

As SirLancelot pointed out, you reset your path rather than augmenting it. You also used single quotes instead of double quotes, so the value set was exactly the string shown, rather than containing the expanded value of $HOME. As noted, the correct solution to that is to use:

正如 SirLancelot 指出的那样,你重置你的路径而不是增加它。您还使用了单引号而不是双引号,因此设置的值正是显示的字符串,而不是包含 $HOME 的扩展值。如前所述,正确的解决方案是使用:

export PATH="$PATH:$HOME/opt/git/bin"

Or you can reverse the order:

或者您可以颠倒顺序:

export PATH="$HOME/opt/git/bin:$PATH"

However, all that does is ensure that when you type git, the executable will be found.

但是,所做的只是确保当您键入 时git,将找到可执行文件。

Your question also mentions using $git; you would have to set that variable, perhaps using:

您的问题还提到使用$git; 您必须设置该变量,也许使用:

export git=$(which git)

Having said that, I don't see an advantage to using $gitwhen gitis on your PATH; it is one extra character to type (and a shifted-digit too). If you want to continue using $git, you probably shouldn't add $HOME/opt/git/binto PATH. Its presence slows down unsuccessful command searches, and if you always access gitvia $git(which would now have to be set using: export git=$HOME/opt/git/bin/git) there is no benefit to having the gitbin directory on your PATH.

话虽如此,我没有看到使用$gitwhen gitis on your PATH的优势;它是一个额外的输入字符(也是一个移位的数字)。如果您想继续使用$git,您可能不应该添加$HOME/opt/git/bin到 PATH。它的存在减慢不成功的命令搜索,如果你始终获得git通过$git(现在就必须使用设置:export git=$HOME/opt/git/bin/git)存在于具有无利益git上的PATH bin目录。



Masi commented about order being meaningless, and Douglas Leeder responded:

Masi 评论说秩序毫无意义,Douglas Leeder 回应道:

The order isn't meaningless - it's the order [in which directories are] searched. However, gitisn't in any of your other search directories, and there shouldn't be any overlap between the commands in the gitbin directory and any others, so the order won't make any difference in this case.

顺序并非毫无意义——它是[目录]搜索的顺序。但是,git不在您的任何其他搜索目录中,并且gitbin 目录中的命令与任何其他目录中的命令之间不应有任何重叠,因此在这种情况下,顺序不会产生任何影响。

That's basically accurate, but I'll spin it a bit. When a command is searched for, the system looks for the program by looking for it in each directory in PATH until it finds it. So, when it looks for ls, for example, with the gitbin directory at the front of the PATH, the shells will look for $HOME/opt/git/bin/lsand not find it, and pass on to other directories in your PATH, eventually finding it in /usr/bin/lsor /bin/ls. Some shells cache the location where a program is found; other's don't. So, it can make sense to keep /binand /usr/binnear the front of your PATH, to speed up access to the standard utilities. I always keep $HOME/binat the front of my PATH; that way, I can override anything with my own version - and I do that for some commands.

这基本上是准确的,但我会稍微旋转一下。搜索命令时,系统会通过在PATH 中的每个目录中查找该程序来查找该程序,直到找到为止。因此,ls例如,当它git使用 PATH 前面的bin 目录$HOME/opt/git/bin/ls查找时,shell 将查找并找不到它,并传递到 PATH 中的其他目录,最终在/usr/bin/ls或 中找到它/bin/ls。一些 shell 会缓存找到程序的位置;别人不。因此,保持/bin/usr/bin靠近 PATH 的前面是有意义的,以加快对标准实用程序的访问。我总是$HOME/bin站在我的 PATH 前面;这样,我可以用我自己的版本覆盖任何东西 - 我对一些命令这样做。

Also, on my main work machine, the /usr/local/bindirectory isn't under my control. I don't trust it, therefore, and I make sure it is right at the end of my PATH, so the antique GCC in it is not the one I use, for example. [Hmmm; they've updated it to 3.4.6; it used to be 2.95 or thereabouts; still, I use 4.3.3.]

此外,在我的主要工作机器上,该/usr/local/bin目录不在我的控制之下。因此,我不相信它,并且我确保它在我的 PATH 的末尾,因此例如,其中的古董 GCC 不是我使用的那个。[嗯;他们已将其更新为 3.4.6;以前是 2.95 左右;仍然,我使用 4.3.3。]

One more suggestion for you. Consider creating a symlink in your $HOME/bin(assuming you have one and it is on your PATH) that points to the install location of git. This means you don't add an extra directory to PATH (so things work marginally faster) but you do get to use the version of gityou choose to use.

再给你一个建议。考虑在你的$HOME/bin(假设你有一个并且它在你的 PATH 上)中创建一个指向git. 这意味着您不会向 PATH 添加额外的目录(因此工作速度会稍微快一些),但您确实可以使用git您选择使用的版本。

ln -s $HOME/opt/git/bin/git $HOME/bin/git

回答by Eduardo

You need to delete the ' ', try this

您需要删除“ ”,试试这个

export PATH=$HOME/opt/git/bin

And to do not overwrite your whole path try this:

并且不要覆盖你的整个路径试试这个:

export PATH=$PATH:$HOME/opt/git/bin

回答by Himadri Choudhury

This should have worked.

这应该有效。

Where does $HOME point to?

$HOME 指向哪里?

Make sure that $HOME/opt/git/bin actually contains an executable file called git.

确保 $HOME/opt/git/bin 实际上包含一个名为 git 的可执行文件。