Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

bash

path

unix

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

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

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

I have also tried commands here.

like image 243
Léo Léopold Hertz 준영 Avatar asked Feb 15 '09 07:02

Léo Léopold Hertz 준영


2 Answers

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

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

export PATH="$HOME/opt/git/bin:$PATH"
like image 70
matpie Avatar answered Sep 22 '22 14:09

matpie


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:

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.

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

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.


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

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 the git bin directory and any others, so the order won't make any difference in this case.

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.

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.]

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.

ln -s $HOME/opt/git/bin/git $HOME/bin/git
like image 38
Jonathan Leffler Avatar answered Sep 22 '22 14:09

Jonathan Leffler