Path of the UNIX Shell

The 1 thing you need to understand

tech unix hack cyberpunk

It's fun to use magick in a game like D&D, and it's arguably even more fun to use it in a techno-centric cyberpunk world like Starfinder or Shadowrun, but in the real world anything that looks like magick can be pretty frustrating. That's because magick is something no one can ever hope to understand. And while it's great to look smart when I sit down to hack on a computer, I want people to understand that a UNIX terminal isn't a dark art. It's plain and scientific technology. It has logic, rules, and laws. And it's something anyone can learn.

To that end, I've been teaching people UNIX for a decade, and if there's one thing that transforms a UNIX terminal from black magick into technology, it's the concept of the PATH. If you understand the PATH, then I promise you, you're 66.6% of the way to knowing how to use the terminal (and as a bonus, you understand the web and a lot of other technologies a lot better, too).

Wait, what's a terminal?

A terminal, or console, used to be a literal keyboard and screen attached by a really long cable to a mainframe computer. People back in the 1970s were really excited about these things because it meant they didn't have to drive in to IBM or their local computer science university to perform a computational task. This was, obviously, well before people had computers in their homes, when the closest thing to a computer that the average person had access to was a Texas Instruments or HP calculator (yes, like that app you never use on your phone).

Today, a terminal is just a software application. It's usually a black window with green text, or similar.

It looks scarier and emptier than it actually is. All a terminal does is run other applications. When you double-clicked on your web browser icon so you could come visit this fine site, you used the graphical version of a terminal. I'm not exaggerating, here. If you wanted to launch a browser in a terminal, you can do that by typing instead of double-clicking:


$ firefox &

That's all a terminal does: it runs software. Sometimes the software it runs happens more or less in the background, so you don't see what's happening until you get back a message in your terminal announcing that it's done. Other times, like launching a web browser, you see the result immediately.

What you need to know about PATH

Once you get past the fear factor of staring into the void that is an empty terminal, the only thing you need to know is that a terminal responds to what you type into it by searching its PATH. The term PATH is just the word used to reserved to represent a location on a hard drive. All computers use paths to find things, whether they tell you or not. You might think that Windows and Macs don't use paths, but they do. Say I open a file called pathfinder_ultimateCampaign.pdf on your PC (literally any PC, even an Apple). If that file lives in my Documents folder, and my Documents folder lives in my home directory on my computer, then the full path of the file is /Users/klaatu/Documents/pathfinder_ultimateCampaign.pdf on a Mac, and C:\Users\klaatu\Documents on Windows.

In other words, if it's on your computer, then it has a file path.

Things you use on the Internet also have file paths. Most web addresses are pretty simple, but the deeper into a site you go, the more down its path you travel. Not all sites show you the path you're traveling, because many are built on frameworks that are meant to take parts of pages and assemble them dynamically, but there are still file paths underneath it all.

How the terminal uses paths

If you could just type any word into a terminal and have it know exactly what you wanted it to do, then that would be magick. However, terminals are not magick, so they only respond to special key words. A terminal knows which words to respond to because of its PATH. A terminal's PATH setting tells it where to look for key words (called commands). If you type a word into a terminal and get a response, then you know that the word appears somewhere in the terminal's path. If not, then you know that the word is not in the terminal's path.

You can see a terminal's path setting:


$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/games

That means that if a command is located in /usr/local/bin or any of the file paths listed (delimited by a colon), then the terminal can find and run it.

More importantly, it means that if you look inside any of those PATH locations, you can see what commands are available to you.

Now, admittedly, most of the commands in a terminal's PATH don't mean anything to you yet. The important thing is that you can see them, and learn more about them, and then use them. It's not magick any more. It's just plain, boring old technology. And you can learn technology.

How you can use paths

Now that you know about the PATH setting, you not only know how to learn a terminal's commands, but you also know how to locate anything on your computer, and how to navigate the internals of your computer from inside a terminal window.

If something is on your computer, it has a file path. Generally, you are placed into your home directory when you start a terminal. You can find that out with the print working directory command, pwd:


$ pwd
/home/klaatu

You can change your working directory at any time with the change directory command, cd:


$ cd Documents
$ pwd
/home/klaatu/Documents

You can list the contents of a directory with the list command, ls, and so on.

All of these actions manipulate your personal, current path.

The terminal itself doesn't move around the way you do. It's a static entity representing the computer as a whole.

Everything on your computer is oriented around the root folder. The root folder is represented as a slash (/). A better name for it would probably be origin or start or something to denote that it is as far back in a path that you can possibly go. In other words, there is nothing before the root of your computer. That means when you want to give yourself or your computer an unmistakable landmark, you can provide a path from the very start of the computer system: the root.

For instance, if you want to describe the location of your Documents folder, you can tell a command the full path:


$ ls /home/klaatu/Documents

It's more common to just describe it in relation to your home, which lazy techies refer to as just a tilde (~) symbol:


$ ls ~/Documents

Or, if your current working directory is Documents, you can omit it entirely:


$ ls

Or as a single dot:


$ ls .

A single dot in a terminal represents your current working directory. A double dot represents the directory behind you.


$ cd ~/Documents
$ ls .
pathfinder_ultimateCampaign.pdf
$ ls ..
Desktop Documents Music Pictures Videos

How to manipulate the terminal path

Just as you can add software to your computer, you can add commands to your terminal. As long as the command you add is in the terminal's path, the terminal can find it and run it. If it's not in the terminal's path, then you must either change the terminal's PATH setting or else override the PATH entirely.

To add a location to the terminal's path, you add a location to the PATH setting in your terminal's start-up script. This can differ depending on what terminal you use, but the most common is GNU Bash, and its start-up file is ~/.bashrc

Say I downloaded the useful command called trashy, and placed it in a directory in my home folder called bin (for binary). This command provides a kind of virtual trash can for my terminal, so that if I want to put a file from my computer into my trash folder, I can do that from the terminal. The terminal doesn't know the command trashy, though:


$ trashy ~/Documents/my-old-file.odt

So I add its location to the terminal's PATH:


$ echo "export PATH=$PATH:/home/klaatu/bin >> ~/.bashrc "

Alternatively, you can open the file ~/.bashrc in a text editor and add this line at the bottom:


$ export PATH=$PATH:/home/klaatu/bin

After you've done that, you must load your new settings:


$ source ~/.bashrc

And now the terminal knows to look in ~/bin for commands not otherwise found in its previous path locations. You can verify that its path has been extended:


$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/games:/home/klaatu/bin

If you don't want to add something to your PATH, then you can just be more explicit with the terminal, using what you know about file paths. For instance, I could execute the trashy script by telling the terminal exactly where to find it:


$ ~/bin/trashy --version 
Trashy, version 2.0 GPLv3

It's not as convenient as just typing trashy and having the terminal do the legwork of finding where the software is, but it's a valid way of telling a terminal to perform a command even though the command isn't integrated into the terminal.

Sometimes a command is not yet executable, meaning that the terminal doesn't have permission to run the file, whether it knows where the file path is or not. To grant a file permission to be run as a command, you must use the change file mode command, chmod:


$ chmod +x ~/bin/trashy

And then run the command, either by adding its location to the terminal's PATH or by providing the path yourself.

Practise is the path to perfection

Now that you know how a terminal determines what words to react to, and you understand how to tell a computer where files exist on your computer, you can practise using file paths yourself. Remember that every file on your computer has a path, no matter what kind of file it is. As you use your computer today, pause for a moment every half hour or so and take a few moments to determine the path of whatever file you're working on. The more you get used to the idea of file paths, the better you will become at understand how computers find the information they need in order to satisfy the many requests it receives from you and from the software it runs. Consequentially, you'll also understand how websites store and access information on servers, how web pages written in HTML find the images and CSS they're meant to display, why buggy software fails to launch (is a DLL file really missing, or is it just in the wrong path?), and much more.

Solarized GNU by Linux Pictures under the idgaf license.

Previous Post Next Post