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.

Terminal
▶️

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

💡
Think of it this way...
When you ask a local "where's the post office?" they know — because it's in the city directory. But "where's Charith's house?" — nobody knows, it's not listed.

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:

🏪
/bin

Essential commands for all users — ls, cp, mv, cat. Required for system operation.

🔧
/sbin

System admin tools — reboot, fsck, ip. Mostly for root/admin use.

🛒
/usr/bin

Most installed programs live here — git, python3, nano, curl.

⚙️
/usr/sbin

System programs for administration — useradd, iptables, sshd.

🏗️
/usr/local/bin

Software YOU installed manually (not via apt). Your personal tools.

🔑
/usr/local/sbin

Your own custom system administration tools.

💡
Think of it this way...
/bin = Ground floor essentials (pharmacy, grocery — things everyone needs)
/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?

On modern Ubuntu (22.04+), /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

/usr/local/sbin/ls✗ No
/usr/local/bin/ls✗ No
/usr/sbin/ls✗ No
/usr/bin/ls✓ Found! Run it.

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:

01

Use ./ (it's right HERE)

sudo ./my-script.sh

./ means 'in the current directory'. The explicit address Linux needs.

02

Full path

sudo /home/charith/scripts/my-script.sh

Give Linux the exact location of the file, like a street address.

03

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:

Terminal
▶️

Click the Play button above to start!

Watch the commands execute step by step

⚠️

Warning

If you forget $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:

Terminal
▶️

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.

💡
Think of it this way...
...but instead of opening a window, it runs inside your terminal. Same idea: open a file, type, save, close. Just without the mouse.
Terminal
▶️

Click the Play button above to start!

Watch the commands execute step by step

Other terminal editors

nanoRecommended

Beginner friendly. Controls shown at bottom. ✅ Use this.

vimAdvanced

Powerful but has a steep learning curve. Modal editing (insert mode, command mode).

emacsExpert

Extremely powerful. More of an IDE than an editor. Massive learning curve.

Key Concepts Flashcards

Click any card to flip and reveal the definition.

What is $PATH?

An environment variable — a colon-separated list of directories where Linux searches for commands when you type them. Run 'echo $PATH' to see yours.

Why can't Linux find my script?

Your personal folder isn't in $PATH. Fix it with ./ (ex: ./myscript.sh), full path (/home/charith/scripts/myscript.sh), or add your folder to $PATH.

What does ./ mean?

Look in the current directory. Since Linux doesn't search your current folder by default (security reason), ./ explicitly says 'the program is right HERE'.

How do you add a folder to $PATH?

export PATH="$PATH:~/your-folder" — the $PATH: part keeps existing folders, then adds yours. Never omit $PATH: or you break all other commands.

How do you make PATH permanent?

Add it to ~/.bashrc: echo 'export PATH="$PATH:~/your-folder"' >> ~/.bashrc — this file runs every time you open a new terminal session.

What is nano?

A terminal text editor — like Notepad but inside the terminal. Open a file: nano filename.txt. Save: Ctrl+O. Exit: Ctrl+X. Essential for servers with no GUI.

Why does /usr/bin exist separately from /bin?

/bin has essential commands needed even when /usr isn't mounted (early boot). /usr/bin has most regular user programs. Modern systems often symlink them, but the distinction is historical.