41

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
  • 59,332
Adam
  • 2,738

3 Answers3

32

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
Jacob Vlijm
  • 85,475
14

$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
  • 122,292
  • 133
  • 301
  • 332
astrob0t
  • 1,766
7

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