Please enable JavaScript eh!

 ⌘ Web Mechanic ⌘ 

Bash Scripting


Paths

Recall previously we discussed how your computer hard drive is functionally similar to a filing cabinet. Now we need to expand that analogy.

There are 2 path terms used in computer usage:
path this one is used generally when discussing where files are on the computer
$PATH this one is an environment variable (UPPER CASE & starts with $)

Like a novice, your computer needs to know precisely 'how?' and 'where?' and 'what?' to do.

As the programmer, you are responsible for giving it ALL the CORRECT information needed to do a particular task.

Learning commands like cp and ls is good, but like a filing cabinet, the files you want to work with could be anywhere on your hard drive. YOU might know where they are, but you have to let each command know as well.

The way to tell a command where a file (or set of files, or directory) exists, is done with the path.

The way to tell the shell where a command is, is through the $PATH.

Confused yet?

We discussed creating a file with the touch command. We made some files, but they were at a specific location: the directory you were in at the time.

The path is the list of directories your command needs to access the files you want to do something with.

This may be a bit of a jump in understanding how your computer organizes files, but it is crucial you get this before you can get much further.

Following the filing cabinet image, we list each directory until we get to the one containing our file/s. We need to list them in the proper order, starting at the outer-most directory and working your way through each of the relevant directories.

The hard drive in your computer is organized this way, with the 'outer-most' container (directory) called root. This is also why we sometimes refer to a 'directory tree', but it's upside down in this case. In a directory tree, we start at the root '/', and work our way down. Each directory is a branch, and a file would be a leaf.

The way we separate each directory on the command line is with a front slash ' / '. On a Windows computer this would be a back slash ' \ '.

On this page we discussed the ls command and how it shows us a list of files in a particular directory.

To see how it looks from the top of the tree, we have to tell it where to start:

ls -1 /System/Applications/Utilities
Activity Monitor.app
AirPort Utility.app
Audio MIDI Setup.app
Bluetooth File Exchange.app
Boot Camp Assistant.app
ColorSync Utility.app
Console.app
Digital Color Meter.app
Disk Utility.app
Grapher.app
Keychain Access.app
Migration Assistant.app
Print Center.app
Screen Sharing.app
Screenshot.app
Script Editor.app
System Information.app
Terminal.app
VoiceOver Utility.app

In this case, the 'top' of the tree is the initial front-slash '/', then each directory name leading to our Application directory, separated by another front-slash. There were no spaces in any of the directory names, so we did not have to use double-quotes around the whole path.

We used the '-1' option to show each entry on a separate line, and '/System/Applications/Utilities' was the argument.

Something you may have (or not) thought about is "When we enter a command into Terminal, why don't we have to tell it where the command is?"

Excellent question. The answer is the shell knows about 2 types of commands:
• internal - which are already part of the shell so doesn't have to look for them
• external - are actual files, but it knows where they are because of a variable known as $PATH

We will talk more about $PATH as an environment variable here.