Skip to main content

What is a command anyway?

A 'command' is usually just a program on the filesystem somewhere. A few commands, however are built-in to the command line.

To find out where a program is, you can use the which command.

For example, let's find out where ls is:

% which ls
/bin/ls

This tells us that ls is a program in /bin. So typing ls is just the same as typing /bin/ls.

Question

Where do some other programs, like echo, cp, or hostname, live? Are they all in /bin?

So how does the command line know which directories to look in? The answer is the $PATH environment variable. To see what's in this, use echo:

%echo $PATH
Note

The directories in $PATH are separated by colons. Can you use tr to turn them into newlines? Hint: '\n' is the way to write a newline character so tr can understand it.

You will probably see that the command line is looking in multiple directories for programs to run - it simply starts at the top and works down the list until it finds a program with the given name. If you want to break your terminal, try resetting $PATH:

% PATH=""
% ls
command not found: ls

Uh-oh! (You'll have to start a new terminal now, or re-set $PATH to the right values, to get it back.)