eolas/zk/The_PATH.md

93 lines
2.6 KiB
Markdown
Raw Normal View History

2022-04-23 13:26:53 +01:00
---
2022-09-06 15:44:40 +01:00
categories:
- Programming Languages
2022-04-23 13:26:53 +01:00
tags:
- shell
---
2023-03-16 08:33:27 +00:00
# The `$PATH`
We know that `$PATH` is an
[environment variable](/Programming_Languages/Shell/Environmental_and_shell_variables.md).
This variable keeps track of directories **where executables are found**.
2022-04-23 13:26:53 +01:00
Whenever any command is run, the shell looks up the directories contained in the
`PATH` for the target executable file and runs it. We can see this is the case
by using the `which` command which traces the executable of bash commands. Take
the `echo` program:
2022-04-23 13:26:53 +01:00
2022-09-06 15:44:40 +01:00
```bash
2022-04-23 13:26:53 +01:00
which echo
2023-03-16 08:33:27 +00:00
# echo: shell built-in command
2022-09-06 15:44:40 +01:00
```
2022-04-23 13:26:53 +01:00
Or `npm` :
2022-09-06 15:44:40 +01:00
```bash
2022-04-23 13:26:53 +01:00
which npm
2023-03-16 08:33:27 +00:00
/home/thomas/.nvm/versions/node/v19.4.0/bin/npm
2022-09-06 15:44:40 +01:00
```
2022-04-23 13:26:53 +01:00
By default the path will always contain the following locations:
2022-09-06 15:44:40 +01:00
- `/usr/bin`
- `/usr/sbin`
- `/usr/local/bin`
- `/usr/local/sbin`
- `/bin`
- `/sbin`
2022-04-23 13:26:53 +01:00
All the inbuilt terminal programs reside at these locations and most of them are
at `/usr/bin`. This is why they run automatically without error. If you attempt
to run a program that doesnt reside at these locations then you will get an
error along the lines of `program x is not found in PATH`.
2022-04-23 13:26:53 +01:00
## Structure of the PATH
2022-09-06 15:44:40 +01:00
```bash
2023-03-16 08:33:27 +00:00
/home/thomas/.nvm/versions/node/v19.4.0/bin:
/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:
/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
2022-09-06 15:44:40 +01:00
```
2022-04-23 13:26:53 +01:00
## Adding to the PATH
Only the default directories load to the PATH on every session. How then can we
add custom directories to the path without them being lost every time we start a
new session? Remember that the user config `.bashrc` loads on init for every
bash session. Therefore, if we set the custom path in this file, it will be
created every time we start a session. This is why when you add a new program it
often ask you to append a script to the bottom of your `.bashrc` .
2022-04-23 13:26:53 +01:00
For example, at the bottom of my `.zshrc` on my work computer I have:
2022-09-06 15:44:40 +01:00
```bash
2022-04-23 13:26:53 +01:00
export CHROME_BIN=/mnt/c/Program\\ Files\\ \\(x86\\)/Google/Chrome/Application/chrome.exe
2022-09-06 15:44:40 +01:00
```
2022-04-23 13:26:53 +01:00
This enables me to access the Chromium binaries from my terminal session (needed
for running Angular tests) but it doesnt add it to the path, it creates an
environment variable on every session.
2022-04-23 13:26:53 +01:00
For demonstration, lets add a users desktop directory to the PATH.
First we go to the `.bashrc` and add the `export` command. Remember that this is
the command for creating a new environment variable:
2022-04-23 13:26:53 +01:00
2022-09-06 15:44:40 +01:00
```bash
2022-04-23 13:26:53 +01:00
export PATH="$PATH=:~/Desktop"
2022-09-06 15:44:40 +01:00
```
2022-04-23 13:26:53 +01:00
We force a reload of the `.bashrc` with the command:
2022-09-06 15:44:40 +01:00
```bash
2022-04-23 13:26:53 +01:00
source ~/.bashrc
2022-09-06 15:44:40 +01:00
```
2022-04-23 13:26:53 +01:00
Then we can check this directory has been added to the path with an echo
2022-09-06 15:44:40 +01:00
```bash
2022-04-23 13:26:53 +01:00
echo $PATH
...:~/Desktop
2022-09-06 15:44:40 +01:00
```