Wednesday, February 4, 2015

What is PATH in Linux and MacOS?

You know all those commands you can automagically summon from any directory on the command line?  Like:
  
$ git <args>

$ apt-get <args>

$ irb

You're probably well aware that each of those commands is a program that could also be executed with:
  
$ ./path/to/git <args>

$ ./path/to/apt-get <args>

$ ./path/to/irb

Without PATH you'd have to execute any program not in your current directory by explicitly declaring the program's path... hence the name. Think of it as a postal system for your shell.  You give your shell a message to deliver to a program.  Your shell flips through its Rolodex of paths known to contain programs to find the recipient and execute it with your message.

Run the following to see where your shell is looking for programs:
  
$ echo $PATH

You should see something similar to:
  
/Users/username/.rbenv/shims:/Users/username/.rbenv/bin:/usr/local/bin:
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin:
/Users/username/.gem/ruby/1.8/bin:/opt/nginx/sbin:/usr/local/sbin

Each path (separated by ':') is where your shell looks for a program to fulfill your ever wish. And in the rare times when your trusty old pal shell can't come through, at least she/he has a sense of humor:
  
$ make love

>> make: *** No rule to make target `love'.  Stop.

This blog is really just a regurgitation of Exercism.io's Understanding PATH so that I retain this awesome knowledge. If you want know how to edit your PATH, visit their article and scroll halfway down.