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

11,484

Solution 1

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> 

Solution 2

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

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

Share:
11,484

Related videos on Youtube

Kevin Burke
Author by

Kevin Burke

I build reliable software and design great experiences. I'm available for hire: https://burke.services

Updated on September 18, 2022

Comments

  • Kevin Burke
    Kevin Burke over 1 year

    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?

    • trojanfoe
      trojanfoe over 11 years
      I bet I can find 50 hits if I search google for this question. Why can't you?
    • Kevin Burke
      Kevin Burke over 11 years
      I'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.
    • Admin
      Admin over 11 years
      And 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.
    • jmort253
      jmort253 over 11 years
      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.
    • Madara's Ghost
      Madara's Ghost over 11 years
      @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.
  • trojanfoe
    trojanfoe over 11 years
    Does this question belong on stackoverflow?
  • Admin
    Admin over 11 years
    @trojanfoe, as much as any shell-related question does, yes, I would think so. It is also a programming language.
  • Joachim Sauer
    Joachim Sauer over 11 years
    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:).
  • Admin
    Admin over 11 years
    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.
  • Joachim Sauer
    Joachim Sauer over 11 years
    Sorry 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
    Joachim Sauer over 11 years
    @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).
  • Yeti
    Yeti over 11 years
    @Joachim Sauer why dont you edit the answer? Why do you prefer to downvote it?
  • Joachim Sauer
    Joachim Sauer over 11 years
    @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).
  • Yeti
    Yeti over 11 years
    edit or post this comment take near the same time, but edit make it better, and help Shantanu Banerjee and other users
  • Joachim Sauer
    Joachim Sauer over 11 years
    @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).
  • Yeti
    Yeti over 11 years
    Im not meaning you to downvote excessively. Its only a point of view - downvote or edit
  • Yeti
    Yeti over 11 years
    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 )
  • trojanfoe
    trojanfoe over 11 years
    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.
  • user1301428
    user1301428 over 11 years
    Why is there a $ before the second PATH in set PATH=/directory/to/add:$PATH:/low/priority/path?
  • user1301428
    user1301428 over 11 years
    Also, couldn't the whole string $PATH: be deleted since the separator is the colon?
  • William Pursell
    William Pursell over 11 years
    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.