Linux Environment
$PATH & the Linux
Command Environment
Ever wonder why typing ls works but your own script doesn't? It all comes down to $PATH — Linux's address book for finding programs.
Sections
6
Read time
15 min
Level
Beginner
What is $PATH?
$PATH is an environment variable — a list of folders that Linux searches when you type a command. It's Linux's address book for finding programs.
Click the Play button above to start!
Watch the commands execute step by step
Those colons : are separators between folder names. Linux checks each folder in order until it finds your command — or says "command not found".
ls = post office (in the directory / $PATH)
./my-script.sh = Charith's house (you give the exact address)
Key Takeaway
$PATH is how Linux knows where to find programs. No $PATH entry = "command not found", even if the file exists.
Folders in $PATH
Each folder in $PATH has a specific purpose. Here's what you'll typically see on Ubuntu:
/binEssential commands for all users — ls, cp, mv, cat. Required for system operation.
/sbinSystem admin tools — reboot, fsck, ip. Mostly for root/admin use.
/usr/binMost installed programs live here — git, python3, nano, curl.
/usr/sbinSystem programs for administration — useradd, iptables, sshd.
/usr/local/binSoftware YOU installed manually (not via apt). Your personal tools.
/usr/local/sbinYour own custom system administration tools.
/sbin = Manager's office (admin staff only)
/usr/bin = Regular shops (most apps you install)
/usr/local/bin = Pop-up shops (software you brought in yourself)
Did You Know?
/bin and /usr/bin are actually the same folder — one is a symlink to the other. The separation is historical, from when /usr could be a separate disk.How PATH Works
When you type a command, Linux walks through $PATH left-to-right, checking each folder until it finds the program or runs out of places to look.
You type: ls
If the command isn't found anywhere in $PATH, you get: command not found. The file might exist on your system — it's just not in a $PATH folder.
Key Takeaway
Linux never searches your entire filesystem. It ONLY looks in the specific folders listed in $PATH. This is both a performance optimization and a security feature.
Running Your Scripts
Your personal scripts are usually in your home folder — which isn't in $PATH. You have three options:
Use ./ (it's right HERE)
sudo ./my-script.sh./ means 'in the current directory'. The explicit address Linux needs.
Full path
sudo /home/charith/scripts/my-script.shGive Linux the exact location of the file, like a street address.
Add your folder to $PATH
export PATH="$PATH:~/scripts"Now you can type my-script.sh directly — Linux will find it.
Modifying PATH
You can add folders to $PATH with the export command. But there's a critical detail that trips up beginners:
Click the Play button above to start!
Watch the commands execute step by step
Warning
$PATH: and write export PATH="~/my-scripts", Linux replaces the entire $PATH with just your folder. Suddenly ls, sudo, grep — everything stops working because Linux can't find them anymore.Anatomy of the export command
export PATH="$PATH:~/my-scripts"
└── OLD PATH ──┘ └─ NEW folder ─┘
Making it permanent
export only lasts for the current terminal session. Close it, it's gone. To survive reboots, add it to ~/.bashrc:
Click the Play button above to start!
Watch the commands execute step by step
Key Takeaway
~/.bashrc is a script that Bash runs every time you open a terminal. Put your PATH changes there to make them permanent.
nano — Your Terminal Text Editor
On a server with no graphical desktop — no VS Code, no mouse — you edit files in the terminal. nano is the friendliest option.
Click the Play button above to start!
Watch the commands execute step by step
Other terminal editors
nanoRecommendedBeginner friendly. Controls shown at bottom. ✅ Use this.
vimAdvancedPowerful but has a steep learning curve. Modal editing (insert mode, command mode).
emacsExpertExtremely powerful. More of an IDE than an editor. Massive learning curve.
Key Concepts Flashcards
Click any card to flip and reveal the definition.