The PATH Variable
How the shell finds programs.
When you type LS, how does the shell know where the LS program is? It doesn't search the whole disk. It checks a short list of folders, stored in a variable called path.

- Where is the ls program?
- Not the whole disk
- A list of folders: PATH
Looking at PATH
Echo dollar path shows a list of folders, separated by colons. The shell checks them in order, from left to right, and runs the first match. Which LS shows where it found LS. In user bin.
echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games which ls /usr/bin/ls
Adding your own folder
Let's make a folder for our own tools, called bin, inside home, and put a script in it. At first, typing its name fails. Command not found. Then we add our bin folder to the front of path. Now the shell finds it, from anywhere.
mkdir -p ~/bin && cp hello.sh ~/bin/hello && chmod +x ~/bin/hello hello bash: hello: command not found export PATH="$HOME/bin:$PATH" hello Hello from my own command!
Making it permanent
That change only lasts until you close the terminal. To keep it, add the export line to the end of your dot bash RC file. Many systems already add home slash bin, or home slash dot local slash bin, to the path automatically, if the folder exists.
| key | value |
|---|---|
| PATH | folders searched for commands |
| which cmd | where it was found |
| export PATH=... | change it now |
| ~/.bashrc | make it permanent |
Summary
Path is the list of folders where the shell looks for commands. Which shows where a command lives, and you can add your own folder to path. Next, aliases, and your bash RC file.
Key takeaways- echo $PATH
- which ls
- export PATH="$HOME/bin:$PATH"