40

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!

5
  • 2
    possible duplicate of How to add a directory to my path?
    – muru
    Nov 20, 2014 at 22:07
  • 2
    @muru No I have read that question and it has nothing in common with mine. Also this question was not helpful at all.
    – Adam
    Nov 20, 2014 at 22:08
  • 4
    @muru I think the question is not about how to add (or I might be misunderstanding what is meant by "Place it on your $PATH"). Nov 20, 2014 at 22:08
  • 1
    @JacobVlijm you are right it is not.
    – Adam
    Nov 20, 2014 at 22:18
  • 2
    I see. Alright. Related; askubuntu.com/q/141718/158442
    – muru
    Nov 20, 2014 at 22:23

3 Answers 3

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)

12
  • we can suggest how to change it in .bashrc PATH=$PATH:/path/you/want, or in .profile, what do you think?
    – Lety
    Nov 20, 2014 at 21:52
  • and this is a useful link for beginners
    – Lety
    Nov 20, 2014 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 :) Nov 20, 2014 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, 2014 at 22:02
  • @Letizia comment = perfect, I will edit. Thanks! Nov 20, 2014 at 22:06
13

$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
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .