Commands have paths, too
What is the one terminal command you use the most? Without a doubt, mine is
git as I frequently make commits while writing articles.
The reason I am asking is because most terminal commands have their own paths in the operating system as well. To find that path, you only have to run
which valid_command_name. For example, here is the path to my
$ which git # In windows, use `where`
You might recognize that the above is an absolute path. But it is a path to what, exactly?
An executable binary file! (If you are in Windows, you will get a
.exe file path)
It contains instructions on what to do when
git commands are run. All terminal commands are shell scripts or executables in some form that perform certain tasks.
For example, you can try the
which command on built-in terminal commands like
touch and you would get absolute paths (for even the
which command itself).
Once you get the path to the executable, you can try running a command with its full path like this:
$ /usr/bin/git status -s # the same as `git status -s`
And it works! But here is what’s interesting — how can the word
git is equivalent to the absolute path
/usr/bin/git? I mean,
git is just a word.
How does your computer know the full executable paths of so many commands and can run them with only a single keyword?
(This is the eureka moment!)
Using the PATH variable!