39

I am trying to install something and among the steps there was this one:

"Place it on your $PATH"

What does this mean? What is that?

I have searched both this site and on Google but everyone just takes it for granted!

Seth
  • 58,122
Adam
  • 2,638

3 Answers3

29

Run in a terminal:

echo $PATH

or

printf "%s\n" "$PATH"

what you see is a list of directories, looking like:

/home/jacob/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

If you put an executable in either one of these directories, you do not need to set the path to the executable / script, but you can run it by its name as a command.

Executables in $PATH should not have a language extension by convention (although they would work)

Editing your $PATH variable

You can (permanently) add a directory to $PATH by adding the following line to your ~/.profile file (invisible by default, press Ctrl+H in the file manager to make it visible):

export PATH=$PATH:/path/to/dir

More usefull information on environment variables

(such as $PATH) can be found here (thanks for the suggestions @Letizia)

bolichep
  • 418
  • 4
  • 5
Jacob Vlijm
  • 83,767
  • we can suggest how to change it in .bashrc PATH=$PATH:/path/you/want, or in .profile, what do you think? – Lety Nov 20 '14 at 21:52
  • and this is a useful link for beginners – Lety Nov 20 '14 at 21:54
  • @Letizia I would prefer the first one. Did you remove your answer? we were pretty much at the same time. we could have made a nicely spread combination :) – Jacob Vlijm Nov 20 '14 at 21:56
  • Yes, I already done, my answer is removed and I thought that is better contribute to your. What do you think about my comment? – Lety Nov 20 '14 at 22:02
  • @Letizia comment = perfect, I will edit. Thanks! – Jacob Vlijm Nov 20 '14 at 22:06
  • Thank you both. This is very helpful. Also Letizia your answer was really good too. – Adam Nov 20 '14 at 22:10
  • @JacobVlijm regarding the extension convention: That is a packaging guideline, not meant for a personal addition. – muru Nov 20 '14 at 22:48
  • @muru sure, but why not keep it? – Jacob Vlijm Nov 20 '14 at 22:50
  • @JacobVlijm sends out a signal as if it is somehow "wrong", when there's nothing wrong with it. – muru Nov 20 '14 at 22:58
  • @muru I don't agree, I think there is no actual difference if it's your own or not, I think you shouldn't use language extensions in $PATH. Ugly in commands as well, but as I said it still does work. – Jacob Vlijm Nov 20 '14 at 23:41
  • @JacobVlijm Sorry, in that case, I must downvote. There is a reason why that recommendation is good for packages, and that reason does not hold for personal programs or personally-installed programs. – muru Nov 20 '14 at 23:42
  • @muru, well do what you think you have to do, I think the signal is correct. – Jacob Vlijm Nov 20 '14 at 23:44
12

$PATH is a environment variable that is file location-related.

When one types a command to run, the system looks for it in the directories specified by PATH in the order specified.

You can view the directories specified by typing echo $PATH in the terminal.

Suppose there is a executable file foobar01.sh present at /home/user/foo1/foo2/foobar01.sh which you want to execute on a regular basis. typing the entire "path" would be time consuming. So we add the directory in to $PATH variable and we can execute foobar.sh directly without even specifying the path.

You can add it to $PATH by typing the following command:

export PATH=$PATH:/home/user/foo1/foo2
karel
  • 114,770
astrob0t
  • 1,746
5

I assume you are coming from a Windows background (apologies if it is not true). In layman's terms, a path (or the search path) is the list of directories that will be searched for anything that you type on the command line. If you type in a built-in command like ls, it will look for a specified list of directories. You can look up your path by typing echo $PATH. Here is one difference between Windows and *nix: By default, Windows always looks for the executable file in the current directory. For example, if you have a file called uptime.bat in c:\myscripts, and you cd c:\myscripts and type in uptime, it will run. However, in *nix, the path will be consulted and the executable found (if available).

If you keep your scripts in a directory called /home/teresa/scripts, to execute those scripts, you will have to specify the full path to that directory. Example: /hone/teresa/checkHost. A variation would be to cd /home/teresa and then type ./checkHost (note the ./ which means that you are explicitly asking the file to run from the current directory.

To avoid this, you can just type

export PATH=$PATH:/home/teresa/scripts

which means that, in addition to the path that already exists now, also search in /hone/teresa/scripts. However, the problem with this is that once you logout, this setting would be gone. So, you should edit the hidden file ~/.bashrc, find the PATH line there, and append it accordingly. I am assuming you use bash. In the case of other shells, the syntax and file are different.

As a new user, it is very tempting to have . in the search path, which basically means that also search in the current directory. However, that is not considered a good practice for reasons discussed elsewhere.

HTH