AI Hints
Home›Linux Tutorial›The Environment
Linux · Lesson 86 of 120

The PATH Variable

How the shell finds programs.

⏱ 2 min read▶ Video at 1:54:56🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

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.

The PATH Variable
From the video: The PATH Variable
  • 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.

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

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

keyvalue
PATHfolders searched for commands
which cmdwhere it was found
export PATH=...change it now
~/.bashrcmake 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"
Next up: Aliases and .bashrc — your own shortcuts