What is the Shell?

Today, most computers have two interfaces:

Most people interact with their computers using the GUI. The Shell is just a program, but it allows us to interact with the computer operating system using text (a command line). A Terminal lets us run a shell.

wikipedia/bash

The heart of a CLI is a read-evaluate-print loop, or REPL: when the user types a command and then presses the enter (or return) key, the computer reads it, executes it, and prints its output. The user then types another command, and so on until the user logs off.

Why learn to use the shell?

Let's Get Started!

Open up a termal window on your machine.

You should have something like:

img Mac Terminal Window

img Mac Terminal Window

The $ at the beginning of a line is called the 'prompt'. Go ahead and type in some commands. What do these do?

whoami
pwd   
ls

Compare your notes with your neighbor. Are there differences?

How does it work?

When we type whoami at the prompt, the shell:

  1. finds a program called whoami,
  2. runs that program,
  3. displays that program's output, then
  4. displays a new prompt to tell us that it's ready for more commands.

Only two letters?

Unix commands are extremely terse. There is good reason for this. When unix was being developed, people interacted with computers through very slow systems like card readers and line printers. Mistakes were extremely expensive. To cut down on typing errors, the designers simply cut down on typing!

The File System

We're all familiar with the file system through whatever desktop environment we use. Using the shell gives a different perspective on the file system, which can be disorienting.

A Finder Window on Mac

With the command line interface, we are effectively looking at the world from 'inside' a directory.

Let's look at an example to learn how to move about a directory tree:

vlad's directory tree

When Vlad runs ls, he can see the files in his home directory, but nothing outside it:

$ ls
data        notes.txt
thesis      tools

He can peek into other directoryies by specifying which one to look into:

$ ls data
one.text    two.txt

When he types pwd, the machine shows the path to his current working directory /users/vlad, which happens to be his home directory.

Absolute Paths Start at Root /

To understand what a "home directory" is, let's have a look at how the file system as a whole is organized.

Filesystem Tree

At the top is the root directory that holds everything else. The command pwd gives the path to the current directory from the root directory. The path starts with / (root), so it is an absolute path.

Have a look again at your own home directory path. What is root on your machine?

echo /users/vlad
echo $HOME`
echo ~
echo pwd
pwd

The command echo prints whatever follows it, but if what follows can be expanded by the shell, it will be. The system has a setting

Relative Paths Start at the Current Directory

Use the command 'cd' to change your current directory. When you change directories, you absolute path changes. Here's what Vlad sees:

pwd
    /users/vlad/
ls data
    one.txt    two.txt
cd data
pwd
    /users/vlad/data/
ls
    one.txt    two.txt

Paths that don't start with / are relative paths, because they are defined relative to the current directory. In ls data in the code above, data is a relative path.

There's also a command for moving up the file hierarchy: cd .. The .. refers to 'parent directory'. A single dot . refers to the current directory. The .. and . aren't shell shortcuts like ~; they are actual directories.

echo ..
echo .
ls -a

The command ls normally ignores filenames that begin with dot, such as .bashrc. However, like many shell commands, ls has options that modify its behaviour. The -a option insists that ls show all files, so it will list all those configuration files you didn't know you had!

Practice time:

Try using cd with different paths, including .. and ., to move around your directories. Use pwd to check where you are, and try out these different options for listing your files and folders:

ls -F
ls -l
ls -al
ls -r
ls -h

A Note About Filenames

As you look around, you'll notice most files have two-part names. The second part of such a name is called the filename extension, and indicates what type of data the file holds: .txt signals a plain text file, .pdf indicates a PDF document, .cfg is a configuration file full of parameters for some program or other, etc.

This is just a convention, but it's a good one to follow. Files contain bytes: our programs need to interpret those bytes correctly to open our PDF documents, images, and mp3 files.

Unix will certainly allow you to rename a PNG image of a whale as whale.mp3, but renaming change the file. However, renaming it might cause the operating system to try to open it with a music player when someone double-clicks it.

Command Review:

ls
cd
pwd
whoami
echo

You've learned to move about the file system!

In []: