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
How can I put $HOME/opt/git/bin to my PATH?
提问by Léo Léopold Hertz ??
回答by matpie
You'll want to be careful with that command. It will overrideyour $PATH.
您需要小心使用该命令。它将覆盖您的 $PATH。
You might need to put it in ~/.bash_profile
and 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 $git
when git
is 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/bin
to PATH. Its presence slows down unsuccessful command searches, and if you always access git
via $git
(which would now have to be set using: export git=$HOME/opt/git/bin/git
) there is no benefit to having the git
bin directory on your PATH.
话虽如此,我没有看到使用$git
when git
is 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,
git
isn't in any of your other search directories, and there shouldn't be any overlap between the commands in thegit
bin directory and any others, so the order won't make any difference in this case.
顺序并非毫无意义——它是[目录]搜索的顺序。但是,
git
不在您的任何其他搜索目录中,并且git
bin 目录中的命令与任何其他目录中的命令之间不应有任何重叠,因此在这种情况下,顺序不会产生任何影响。
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 git
bin directory at the front of the PATH, the shells will look for $HOME/opt/git/bin/ls
and not find it, and pass on to other directories in your PATH, eventually finding it in /usr/bin/ls
or /bin/ls
. Some shells cache the location where a program is found; other's don't. So, it can make sense to keep /bin
and /usr/bin
near the front of your PATH, to speed up access to the standard utilities. I always keep $HOME/bin
at 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/bin
directory 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 git
you 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 的可执行文件。