Chapter 1

Introduction to Unix, Linux and X

Introduction 2013

These lecture notes were written in 1994 at Sheffield Hallam University for students who were using a Unix server to learn about using Unix's command line. I keep them around because they are just as correct and useful as they were when first written.

In 1994, my students used Solaris (Sun's version of Unix), the Motif window manager and the xterm tool. Solaris allowed hundreds of students to share one Unix system. These notes started by telling people how to log on and off.

Today, the user is probably using Linux on a personal computer. He or she is probably using Gnome or KDE. They have fired up a terminal -- probably not an xterm -- and are staring at a shell prompt. At the terminal level all the commands are almost unchanged. Where there are real differences, they will be pointed out with a heading like the one above.

In 1994, students used the Bourne shell; now, most readers will be using bash -- the Bourne-again shell. This also makes no difference to these notes -- everything here also applies unchanged to bash.

In what follows, wherever it says "Unix" you can treat it as "Linux" or "Linux / Unix".

Introduction

In this chapter, we see what Unix is and meet a few simple commands.

Unix

Unix is the only operating system which runs on any computer from any manufacturer. It is over forty years old now but it is still worth learning; this is partly because it is so widely used and partly because many of its novel features are now accepted practice in modern operating systems. Perhaps the best thing about it is that it was written by two programmers for their own use. They wrote it because the alternatives of the time were inflexible, idiosyncratic and down-right unpleasant to use. Unix is a little idiosyncratic too, but many programmers today still find it a joy to use compared with any of its more modern counterparts.

The X Window System

The X Window System is the most powerful windowing system available. It isn't used in office equipment but is used in specialised, expensive applications and by computer professionals themselves. It is the de facto standard for windowing on Unix and Linux systems. Later in the book we will see what facilities X provides, but to start with, we will just use the system without worrying about whether a facility is provided by Unix or by X.

The Bourne shell

We will see, later in this chapter, what a shell is. Of the many shell's available, the Bourne shell is the easiest to learn and find out about; it is also the most widely available.

This book assumes you will be using Unix through the Bourne shell via the X Window System. If you don't have access to all of these, you need to find another book or to persuade your system manager to provide them for you.

The big ideas

Many of what were the more novel features of Unix have become common practice in modern operating systems. We will refer to these features as the big ideas; none of them was invented by the authors of Unix but they did bring them together from various sources into the first versions of Unix.

The big ideas are:

They may seem meaningless now but they will help to provide structure as you read the book. We will examine each of them in more detail in later chapters.

Command prompt

When you have started a terminal you should see a window a bit like this:


Diagram of an xterm

On my computer, the terminals appear thus:


Diagram of an xterm

The windows are both called xterms. At the top left corner of the xterm you should see a dollar sign ($) followed by a small rectangle. It is a prompt to show you that Unix is ready to execute a command. The small rectangle is a typing cursor and should not be confused with the mouse cursor which tracks mouse movements on the screen. The program that displays the prompt is called a shell. There are several different shells available and they have different prompts. The one whose prompt is a dollar sign is called the Bourne shell. We are learning it because it is described in all books on Unix and is the easiest to learn.

If you don't have a dollar sign but have something like this: [41]% instead it is because your system manager has given you another shell called the C-shell. You can try using the chsh command to tell Unix you want the Bourne shell in future. If that does not work, you will have to ask the system manager to provide you with the Bourne shell.

Shell commands

As well a showing a prompt, the shell executes Unix commands for you. You have to type the name of the command and press the return key. When you type the name of the command the characters will appear in the xterm after the prompt. If the characters do not appear it will be because the mouse cursor is not inside the xterm. If you move the mouse cursor inside the xterm you will see the typing cursor change from a hollow box to a solid one. You will also see the frame around the xterm window change colour. Try moving the mouse cursor in and out of the window and watch what happens.

Next let's try typing our first Unix command - the date command.

$ date
Fri Jun 16 15:37:56 BST 1995
$

The date command produces just one line of output. The dollar sign after it is the Bourne shell's indication that you can now enter another command.

Throughout the book, words that look like this: word are words that have been entered into Unix or displayed by Unix. Unix commands will look like that, whenever they appear in the text.

If nothing happened when you typed date it was because you did not press the return key which shows that you have finished typing the line. Most Unix commands can be typed on one line.

Now we enter another command:

$ cal
   June 1995
 S  M Tu  W Th  F  S
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30

$

The cal command shows you a calendar for the current month.

Unchanged since 1969

At first glance it seems daft to be looking at output from 1995 but think about it. There was no need to learn a different command! Over the years the programme's coding may have been improved or corrected but to the user it still appears the same.

Also, in 2013 the Linux version of the command changed so that if the current day is displayed it is highlighted by being displayed in reverse video. Amazing -- a forty year old programme is still being cared for and improved!

By the way the Linux version differs very slightly from the Unix one -- the days are denoted by: Su Mo Tu We Th Fr Sa instead of: S M Tu W Th F S.

Correcting mistakes

If we make a mistake while typing the name of a Unix command, we can correct it by pressing the key that rubs out the character just typed. This varies from system to system. Mine is at the top right of the main keypad and is marked with an left pointing arrow; when it is pressed it removes the last character from the line. The only line that can be corrected in an xterm is the last one and then only if we have not pressed the return key to end the line. When the return key is pressed the command is obeyed and it is too late to correct the mistakes by rubbing out characters.

There are often four other keys marked with arrows; they should not be used with xterms. If these keys echo like this when you type them:

$ ^[[A^[[B^[[D^[[C

you are unlikely to be troubled by them. On some systems, they cause the typing cursor to go up, down, left and right respectively while leaving the screen unmarked. They can then be a nuisance until you learn not to use them.

Setting the rubout key

If the rubout key isn't set up correctly on your system you can correct it for the current window by using this command:

$ stty erase X
$

The example would make the X key the rubout key. Clearly this would be impractical. You should instead, hit the key you want to use for rubbing out the previous character.

Command names must be exact

If we do not type the command names exactly as shown, Unix will not execute them. For example:

$ DATE
DATE: not found
$

or perhaps:

$ Date
Date: not found
$

The error messages mean that there is no such command as DATE or Date. If your system does not echo the four arrow keys mentioned in the last section, try typing them before the date command; you should see this message:

$ date
^[[A^[[B^[[D^[[Cdate: not found
$

We will see later why Unix says not found instead of `no such command'.

Stopping a command

Stopping a command is easy - you hold down the "control" or "Ctrl" key and type a "C". The command is interrupted and the shell prompts you for the next command. Here nothing much is going on but we stop it anyway:

$ ^C
$

See how the control C is echoed.

Who else is logged in?

There is another command that consists of just one word that we can use right now. It tells us who is logged on to our Unix system. For example:

$ who
acsgae   ttyp2   Jun 16 09:03   (:ttyp0:S.1)
stgdc    ttyp3   Jun 16 16:47   (ho-5221-007.csv.)
acsgjm   ttypb   Jun 16 09:52   (hbp-a244-07.csv.)
cmsps    ttype   Jun 16 13:21   (pscott.cms.shu.a)
cmsps    ttyq4   Jun 16 15:15   (pscott.cms.shu.a)
acsgjm   ttyq5   Jun 16 15:16   (hbp-a244-07.csv.)
bjsoar   ttyq9   Jun 16 13:52   (hbp-a246-13.csv.)
$

The exact output varies from system to system but the first word on each line is the user's login code.

Multi-command lines

You can put several commands on one line if you put a semi-colon (;) between them. You can put spaces after, or even before, the semi-colon if you like.

$ date; cal    ; date
Fri Jun 16 17:01:40 BST 1995
   June 1995
 S  M Tu  W Th  F  S
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30

Fri Jun 16 17:01:40 BST 1995
$

As you see, there is nothing in the output to show that it was produced by three separate commands.

Multi-line commands

Unix commands can spread over several lines if needed. Here is an example:

$ da\
> te
Wed Mar  5 09:13:24 GMT 1997
$

The backslash (\) before the first newline tells Unix the rest of the command follows on the next line so the command is date not da. Notice that Unix uses a different prompt (>) for continuation lines. We have deliberately made Unix use its secondary prompt; often it happens accidentally when you use quotation marks and forget to pair them off properly.

Arguments

Most Unix commands need more information to specify what they are to do. This interchange between Unix and a user shows a command with extra information:

$ cal 11 1947
   November 1947
 S  M Tu  W Th  F  S
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
$

The name of the command must come first; the extra information is entered on the same line as the command, after the command itself. We call this information the command's arguments. So, in the example, 11 was the first argument and 1947 was the second argument to cal. There must be at least one space character between the command name and the first argument. There must be at least one space character between each argument and the next. By the way, cal 11 47 would produce different output from cal 11 1947. The 47 is taken to be 47AD!

Default behaviour

Notice that when it is given arguments, cal behaves differently to when it has none. The behaviour without arguments is called the default behaviour. The authors of Unix take great pains to ensure the default behaviour of commands is both useful and sensible.

Effect of different number of arguments

This example shows that who also behaves differently with different numbers of arguments:

$ who am i
holly!cmsps    ttyp5   Jun 19 13:37      (pscott.cms.shu.a)
$

With two arguments, who tells you about yourself. The numbers of arguments is the only thing who is bothered about; it gives exactly the same output with who are you!

The cal command also behaves differently with a different number of arguments. Try: cal 1947 to see the effect.

QUESTIONS

  1. Make Unix display today's date

    Answer

    $ date
    Fri Jun 30 15:50:05 BST 1995
    $
    

    Hide

  2. Make Unix display a calendar for the year in which you were born.

    Answer

    $ cal 1947
                                   1947                               
    
           January               February                 March
              1  2  3  4                      1                      1
     5  6  7  8  9 10 11    2  3  4  5  6  7  8    2  3  4  5  6  7  8
    .
    (24 lines omitted)
    .
    26 27 28 29 30 31      23 24 25 26 27 28 29   28 29 30 31
                           30
    
    $
    

    Hide

  3. Look if anyone else is logged on to your system.

    Answer

    $ who
    cmsps    :0           Jun 28 12:34 (:0)
    cmsps    pts/0        Jun 28 15:26 (:0)
    cmsps    pts/1        Jun 28 15:48 (:0)
    $
    

    No -- just me!

    Hide

  4. Run four date commands on one line.

    Answer

    $ date; date; date; date
    Fri Jun 30 15:54:45 BST 1995
    Fri Jun 30 15:54:45 BST 1995
    Fri Jun 30 15:54:45 BST 1995
    Fri Jun 30 15:54:46 BST 1995
    $
    

    Hide

  5. Run the date command but spread it over four lines.

    Answer

    $ d\
    > a\
    > t\
    > e
    Fri Jun 30 15:55:07 BST 1995
    $
    

    Hide

ANSWERS

  1. $ date
    Fri Jun 30 15:50:05 BST 1995
    $
    
  2. $ cal 1947
                                   1947                               
    
           January               February                 March
              1  2  3  4                      1                      1
     5  6  7  8  9 10 11    2  3  4  5  6  7  8    2  3  4  5  6  7  8
    .
    (24 lines omitted)
    .
    26 27 28 29 30 31      23 24 25 26 27 28 29   28 29 30 31
                           30
    
    $
    
  3. $ who
    cmsps    :0           Jun 28 12:34 (:0)
    cmsps    pts/0        Jun 28 15:26 (:0)
    cmsps    pts/1        Jun 28 15:48 (:0)
    $
    

    No -- just me!

  4. $ date; date; date; date
    Fri Jun 30 15:54:45 BST 1995
    Fri Jun 30 15:54:45 BST 1995
    Fri Jun 30 15:54:45 BST 1995
    Fri Jun 30 15:54:46 BST 1995
    $
    
  5. $ d\
    > a\
    > t\
    > e
    Fri Jun 30 15:55:07 BST 1995
    $