What is the Unix PATH variable and how do I add to it?

11

3

Many programming tutorials ask users to add something to their PATH but don't explain what it is. The existing resources that explain the PATH to users new to the command line are not very good.

  • What is the Unix PATH variable?

  • I just downloaded a program, then tried to run a command in the terminal and got command not found. What does this have to do with the PATH?

  • I added something to the PATH with this command and then things worked:

    export PATH=/path/to/some/bin:$PATH
    

    ...but then I got command not found again the next time I started my computer. How can I make it stay on the PATH every time I open the Terminal?

  • What's the best way to determine if a program like Git or gcc is already loaded on my computer?

Kevin Burke

Posted 2012-12-11T08:00:43.990

Reputation: 809

3I've read through many of the answers on SO and I haven't found a answer that answers every one of these questions in a way a newbie can understand. I want something I can link new command line users to. It was my hope to add this as community wiki and attach significant bounty to it. – Kevin Burke – 2012-12-11T08:02:05.127

4I bet I can find 50 hits if I search google for this question. Why can't you? – trojanfoe – 2012-12-11T08:02:05.777

2And if none of those links points to SO for a question that's qualified to be here, then it should be added here. We want SO to be where Google goes for programming questions. – None – 2012-12-11T08:19:18.677

Please see this MSO post for Kevin's motivations in asking this question. He'll most likely make an [edit] to clarify how this information is helpful.

– jmort253 – 2012-12-11T08:41:16.387

1@jmort253: Well, he should start doing it quickly. 4/5 close votes. I won't close because I know wants to edit the question, but he better make it quick. – Madara's Ghost – 2012-12-11T09:03:41.517

Answers

17

The UNIX path is an environment variable which is a list of directories in which to look for programs that you're trying to run. It allows you to avoid having to use the complete pathname for running things like /bin/ls (for example by putting /bin in the path).

For example, a path may consist of:

/bin:/usr/bin:/usr/sbin

and that means, when you type in the command xyzzy, it will try to run the first file it finds from the current list:

/bin/xyzzy
/usr/bin/xyzzy
/usr/sbin/xyzzy

(it may skip non-executable files if it's being clever).

You can add things to the path with a command like:

set PATH=/directory/to/add:$PATH:/low/priority/path

which places /directory/to/add at the start of the path search list, and /low/priority/path at the end.

However, this usually only changes for the current shell. If you want to make a change in every shell, you should add that line to one of your startup files, like $HOME/.profile or /etc/profile. The correct file to use depends on your shell itself and how you've set up the startup files. It's not always easy to tell where it should go but the rules are generally explained in the manpage for whatever shell you're using.

You can usually find a command in the path with one of:

which cmd
whence cmd

to locate the cmd executable. For example, on my Debian system, I get the following transcript:

pax> which ls
/bin/ls

pax> which firefox
/usr/bin/firefox

pax> which xyzzy

pax> 

user53528

Posted 2012-12-11T08:00:43.990

Reputation:

1Does this question belong on stackoverflow? – trojanfoe – 2012-12-11T08:06:21.543

@trojanfoe, as much as any shell-related question does, yes, I would think so. It is also a programming language. – None – 2012-12-11T08:08:51.973

I modified the answer to use : as the separator, as that's used on *nix. Windows uses ; (probably because : already has a meaning in C:). – Joachim Sauer – 2012-12-11T08:57:41.637

Actually, @Joachim, it's possibly more correct to say that a given shell (rather than UNIX itself) has a specific directory separator but you're right for all the ones I use off the top of my head. Thanks for the fix, cheers. – None – 2012-12-11T08:59:26.753

@paxdiablo: yes, I thought about that but I too can't think of any unix shell that doesn't use : (in Java, for example path.separator is always : on *nix). – Joachim Sauer – 2012-12-11T09:01:16.397

Most of the time the shell is used interactively, and not to run scripts, and therefore non-developers have an interest in setting $PATH. The examples cited in the question are all interactive. Therefore I think this question belongs on a non-developer site. – trojanfoe – 2012-12-11T11:01:02.830

Why is there a $ before the second PATH in set PATH=/directory/to/add:$PATH:/low/priority/path? – user1301428 – 2012-12-11T16:08:51.663

Also, couldn't the whole string $PATH: be deleted since the separator is the colon? – user1301428 – 2012-12-11T16:14:28.927

In csh, you can run set PATH=/directory/to/add:$PATH:/low/priority/path. However, in the sh family (bash, ksh, zsh, etc), that does not set the variable PATH at all, but assigns to $1. – William Pursell – 2012-12-11T23:03:26.500

5

What is?

The PATH environment variable is a colon-delimited list of directories that your shell searches through when you enter a command.

How to set it?

PATH=$PATH:/your/directory
export PATH

How can I make it stay on the PATH every time I open the Terminal?

Put the previous two lines inside ~/.bash_profile (if you are using bash for the Terminal).

Pigueiras

Posted 2012-12-11T08:00:43.990

Reputation: 151

3

PATH is a environment variable for unix like systems.

set path:

export PATH=$PATH:<your path>

unset path:

unset $PATH

set path permanently

in your home folder, enable View --> Show Hidden Files.... pen .bash_profile file, before export PATH line, add this line.....

PATH=$PATH:<yourpath>

logout and login again...... check if its working ... well ! it should work.....

Shantanu Banerjee

Posted 2012-12-11T08:00:43.990

Reputation: 139

1Sorry for the downvote, but this is poorly formatted and phrased. Try to avoid excessive use of bold, excessive use of ellipsis ("...") and write full sentences. That should help avoid such downvotes in the future. – Joachim Sauer – 2012-12-11T09:00:01.093

1@Joachim Sauer why dont you edit the answer? Why do you prefer to downvote it? – None – 2012-12-11T09:01:37.037

1@MikroDel: because there are already better, higher-quality answers to this question and downvoting takes less effort. If this where the only answer, I would probably have edited it. Also: although they are usually no fun, downvotes are a legitimate tools (yes, there are up and down arrows next to each question/answer). – Joachim Sauer – 2012-12-11T09:03:35.713

1edit or post this comment take near the same time, but edit make it better, and help Shantanu Banerjee and other users – None – 2012-12-11T09:09:33.470

@MikroDel: so explaining the reasoning behind a downvote doesn't help the user? I think I've been pretty clear on my reasoning and have given practical tips on improving the quality. The only thing your insistence does it reduce my willingness to explain my downvotes (and as you can see in my profile I don't downvote excessively). – Joachim Sauer – 2012-12-11T09:13:57.203

Im not meaning you to downvote excessively. Its only a point of view - downvote or edit – None – 2012-12-11T09:22:03.853

You have commented you downvote it "+" I mean. Many user downvote something without any explanation. So my preference 1. Edit, 2. Downvote with explanation 3. Flag ) – None – 2012-12-11T09:23:52.793