(from Spinnelis lesson) beginners
(from Spinnelis lesson) beginners
Brian W. Kernighan
Bell Laboratories
Murray Hill, New Jersey 07974
ABSTRACT
This paper is meant to help new users get started on the UNIX†
operating system. It includes:
• basics needed for day-to-day use of the system — typing commands, correcting typing mistakes, logging in and
out, mail, inter-terminal communication, the file system, printing files, redirecting I/O, pipes, and the shell.
• document preparation — a brief discussion of the major formatting programs and macro packages, hints on
preparing documents, and capsule descriptions of some supporting software.
• UNIX programming — using the editor, programming the shell, programming in C, other languages and tools.
• An annotated UNIX bibliography.
November 2, 1997
Brian W. Kernighan
Bell Laboratories
Murray Hill, New Jersey 07974
-2-
-3-
UNIX provides a postal system so you can communi- A protocol is needed to keep what you type from
cate with other users of the system. To read your mail, getting garbled up with what Joe types. Typically it’s
type the command like this:
mail Joe types write smith and waits.
Smith types write joe and waits.
Your mail will be printed, one message at a time, most
Joe now types his message (as many lines as he
recent message first. After each message, mail waits
likes). When he’s ready for a reply, he signals
for you to say what to do with it. The two basic
it by typing (o), which stands for ‘‘over’’.
responses are d, which deletes the message, and
Now Smith types a reply, also terminated by
RETURN, which does not (so it will still be there the
(o).
next time you read your mailbox). Other responses are
This cycle repeats until someone gets tired; he
described in the manual. (Earlier versions of mail do
then signals his intent to quit with (oo), for
not process one message at a time, but are otherwise
‘‘over and out’’.
similar.)
To terminate the conversation, each side must
How do you send mail to someone else? Suppose
type a ‘‘control-d’’ character alone on a line.
it is to go to ‘‘joe’’ (assuming ‘‘joe’’ is someone’s login
(‘‘Delete’’ also works.) When the other person
name). The easiest way is this:
types his ‘‘control-d’’, you will get the message
mail joe EOF on your terminal.
now type in the text of the letter
If you write to someone who isn’t logged in, or
on as many lines as you like ...
who doesn’t want to be disturbed, you’ll be told. If the
After the last line of the letter
target is logged in but doesn’t answer after a decent
type the character ‘‘control−d’’,
interval, simply type ‘‘control-d’’.
that is, hold down ‘‘control’’ and type
a letter ‘‘d’’.
On-line Manual
And that’s it. The ‘‘control-d’’ sequence, often called The UNIX Programmer’s Manual is typically kept
‘‘EOF’’ for end-of-file, is used throughout the system to on-line. If you get stuck on something, and can’t find
mark the end of input from a terminal, so you might as an expert to assist you, you can print on your terminal
well get used to it. some manual section that might help. This is also use-
For practice, send mail to yourself. (This isn’t as ful for getting the most up-to-date information on a
strange as it might sound — mail to oneself is a handy command. To print a manual section, type ‘‘man com-
reminder mechanism.) mand-name’’. Thus to read up on the who command,
There are other ways to send mail — you can send type
a previously prepared letter, and you can mail to a num-
man who
ber of people all at once. For more details see mail(1).
(The notation mail(1) means the command mail in sec- and, of course,
tion 1 of the UNIX Programmer’s Manual.)
man man
Writing to other users tells all about the man command.
At some point, out of the blue will come a mes-
sage like Computer Aided Instruction
Your UNIX system may have available a program
Message from joe tty07...
called learn, which provides computer aided instruc-
accompanied by a startling beep. It means that Joe tion on the file system and basic commands, the editor,
wants to talk to you, but unless you take explicit action document preparation, and even C programming. Try
you won’t be able to talk back. To respond, type the typing the command
command
learn
write joe
If learn exists on your system, it will tell you what to
This establishes a two-way communication path. Now do from there.
whatever Joe types on his terminal will appear on yours
and vice versa. The path is slow, rather like talking to II. DAY-TO-DAY USE
the moon. (If you are in the middle of something, you
have to get to a state where you can type a command. Creating Files — The Editor
Normally, whatever program you are running has to ter- If you have to type a paper or a letter or a pro-
minate or be terminated. If you’re editing, you can gram, how do you get the information stored in the
escape temporarily from the editor — read the editor machine? Most of these tasks are done with the UNIX
tutorial.) ‘‘text editor’’ ed. Since ed is thoroughly documented
in ed(1) and explained in A Tutorial Introduction to the
-- --
-4-
UNIX Text Editor, we won’t spend any time here but other variations are possible. For example, the
describing how to use it. All we want it for right now is command
to make some files. (A file is just a collection of infor-
ls −t
mation stored in the machine, a simplistic but adequate
definition.) causes the files to be listed in the order in which
To create a file called junk with some text in it, do they were last changed, most recent first. The −l
the following: option gives a ‘‘long’’ listing:
ed junk (invokes the text editor) ls −l
a (command to ‘‘ed’’, to add text)
will produce something like
now type in
whatever text you want ... −rw−rw−rw− 1 bwk 41 Jul 22 2:56 junk
. (signals the end of adding text) −rw−rw−rw− 1 bwk 78 Jul 22 2:57 temp
The ‘‘.’’ that signals the end of adding text must be at The date and time are of the last change to the file.
the beginning of a line by itself. Don’t forget it, for The 41 and 78 are the number of characters (which
until it is typed, no other ed commands will be recog- should agree with the numbers you got from ed).
nized — everything you type will be treated as text to bwk is the owner of the file, that is, the person
be added. who created it. The −rw−rw−rw− tells who has
At this point you can do various editing operations permission to read and write the file, in this case
on the text you typed in, such as correcting spelling everyone.
mistakes, rearranging paragraphs and the like. Finally, Options can be combined: ls −lt gives the
you must write the information you have typed into a same thing as ls −l, but sorted into time order. You
file with the editor command w: can also name the files you’re interested in, and ls
will list the information about them only. More
w
details can be found in ls(1).
ed will respond with the number of characters it wrote The use of optional arguments that begin with
into the file junk. a minus sign, like −t and −lt, is a common conven-
Until the w command, nothing is stored perma- tion for UNIX programs. In general, if a program
nently, so if you hang up and go home the information accepts such optional arguments, they precede any
But after w the information is there permanently; filename arguments. It is also vital that you sepa-
you can re-access it any time by typing rate the various arguments with spaces: ls−l is not
the same as ls −l.
ed junk
Type a q command to quit the editor. (If you try to Printing Files
quit without writing, ed will print a ? to remind Now that you’ve got a file of text, how do you
you. A second q gets you out regardless.) print it so people can look at it? There are a host
Now create a second file called temp in the of programs that do that, probably more than are
same manner. You should now have two files, needed.
junk and temp. One simple thing is to use the editor, since
printing is often done just before making changes
What files are out there? anyway. You can say
The ls (for ‘‘list’’) command lists the names
ed junk
(not contents) of any of the files that UNIX knows
1,$p
about. If you type
ed will reply with the count of the characters in
ls
junk and then print all the lines in the file. After
the response will be you learn how to use the editor, you can be selec-
tive about the parts you print.
junk
There are times when it’s not feasible to use
temp
the editor for printing. For example, there is a
which are indeed the two files just created. The limit on how big a file ed can handle (several thou-
names are sorted into alphabetical order automati- sand lines). Secondly, it will only print one file at
cally, a time, and sometimes you want to print several,
is lost.† one after another. So here are a couple of alterna-
tives.
† This is not strictly true — if you hang up while First is cat, the simplest of all the printing
editing, the data you were working on is saved in a programs. cat simply prints on the terminal the
file called ed.hup, which you can continue with at contents of all the files named in a list. Thus
your next session.
cat junk
−− −−
-5-
prints one file, and will remove both of the files named.
You will get a warning message if one of the
cat junk temp
named files wasn’t there, but otherwise rm, like
prints two. The files are simply concatenated most UNIX commands, does its work silently.
(hence the name ‘‘cat’’) onto the terminal. There is no prompting or chatter, and error mes-
pr produces formatted printouts of files. As sages are occasionally curt. This terseness is
with cat, pr prints all the files named in a list. The sometimes disconcerting to newcomers, but expe-
difference is that it produces headings with date, rienced users find it desirable.
time, page number and file name at the top of each
page, and extra lines to skip over the fold in the What’s in a Filename
paper. Thus, So far we have used filenames without ever
saying what’s a legal name, so it’s time for a cou-
pr junk temp
ple of rules. First, filenames are limited to 14 char-
will print junk neatly, then skip to the top of a new acters, which is enough to be descriptive. Second,
page and print temp neatly. although you can use almost any character in a
pr can also produce multi-column output: filename, common sense says you should stick to
ones that are visible, and that you should probably
pr −3 junk
avoid characters that might be used with other
prints junk in 3-column format. You can use any meanings. We have already seen, for example, that
reasonable number in place of ‘‘3’’ and pr will do in the ls command, ls −t means to list in time
its best. pr has other capabilities as well; see order. So if you had a file whose name was −t,
pr(1). you would have a tough time listing it by name.
It should be noted that pr is not a formatting Besides the minus sign, there are other characters
program in the sense of shuffling lines around and which have special meaning. To avoid pitfalls, you
justifying margins. The true formatters are nroff would do well to use only letters, numbers and the
and troff, which we will get to in the section on period until you’re familiar with the situation.
document preparation. On to some more positive suggestions. Sup-
There are also programs that print files on a pose you’re typing a large document like a book.
high-speed printer. Look in your manual under Logically this divides into many small pieces, like
opr and lpr. Which to use depends on what equip- chapters and perhaps sections. Physically it must
ment is attached to your machine. be divided too, for ed will not handle really big
files. Thus you should type the document as a
Shuffling Files About number of files. You might have a separate file for
Now that you have some files in the file sys- each chapter, called
tem and some experience in printing them, you can
chap1
try bigger things. For example, you can move a
chap2
file from one place to another (which amounts to
etc...
giving it a new name), like this:
Or, if each chapter were broken into several files,
mv junk precious
you might have
This means that what used to be ‘‘junk’’ is now
chap1.1
‘‘precious’’. If you do an ls command now, you
chap1.2
will get
chap1.3
precious ...
temp chap2.1
chap2.2
Beware that if you move a file to another one that
...
already exists, the already existing contents are lost
forever. You can now tell at a glance where a particular file
If you want to make a copy of a file (that is, to fits into the whole.
have two versions of something), you can use the There are advantages to a systematic naming
cp command: convention which are not obvious to the novice
UNIX user. What if you wanted to print the whole
cp precious temp1
book? You could say
makes a duplicate copy of precious in temp1.
pr chap1.1 chap1.2 chap1.3 ......
Finally, when you get tired of creating and
moving files, there is a command to remove files but you would get tired pretty fast, and would
from the file system, called rm. probably even make mistakes. Fortunately, there is
a shortcut. You can say
rm temp temp1
-- --
-6-
-7-
You should get a response something like this See ls(1) and chmod(1) for details. As a matter of
(although again the details may be different): observed fact, most users most of the time find
openness of more benefit than privacy.
bin
As a final experiment with pathnames, try
dev
etc ls /bin /usr/bin
lib
Do some of the names look familiar? When you
tmp
run a program, by typing its name after the prompt
usr
character, the system simply looks for a file of that
This is a collection of the basic directories of files name. It normally looks first in your directory
that the system knows about; we are at the root of (where it typically doesn’t find it), then in /bin and
the tree. finally in /usr/bin. There is nothing magic about
Now try commands like cat or ls, except that they have
been collected into a couple of places to be easy to
cat /usr/your-name/junk
find and administer.
(if junk is still around in your directory). The What if you work regularly with someone
name else on common information in his directory? You
could just log in as your friend each time you want
/usr/your-name/junk
to, but you can also say ‘‘I want to work on his
is called the pathname of the file that you nor- files instead of my own’’. This is done by chang-
mally think of as ‘‘junk’’. ‘‘Pathname’’ has an ing the directory that you are currently in:
obvious meaning: it represents the full name of the
cd /usr/your-friend
path you have to follow from the root through the
tree of directories to get to a particular file. It is a (On some systems, cd is spelled chdir.) Now
universal rule in the UNIX system that anywhere when you use a filename in something like cat or
you can use an ordinary filename, you can use a pr, it refers to the file in your friend’s directory.
pathname. Changing directories doesn’t affect any permis-
Here is a picture which may make this sions associated with a file — if you couldn’t
clearer: access a file from your own directory, changing to
(root) another directory won’t alter that fact. Of course,
/|\ if you forget what directory you’re in, type
/ | \
/ | \ pwd
bin etc usr dev tmp to find out.
/|\ /|\ /|\ /|\ /|\
/ | \ It is usually convenient to arrange your own
/ | \ files so that all the files related to one thing are in a
adam eve mary directory separate from other projects. For exam-
/ / \ \ ple, when you write your book, you might want to
/ \ junk
junk temp keep all the text in a directory called book. So
make one with
Notice that Mary’s junk is unrelated to Eve’s.
This isn’t too exciting if all the files of inter- mkdir book
est are in your own directory, but if you work with then go to it with
someone else or on several projects concurrently, it
becomes handy indeed. For example, your friends cd book
can print your book by saying then start typing chapters. The book is now found
pr /usr/your-name/chap* in (presumably)
Similarly, you can find out what files your neigh- /usr/your-name/book
bor has by saying To remove the directory book, type
ls /usr/neighbor-name rm book/*
or make your own copy of one of his files by rmdir book
cp /usr/your-neighbor/his-file yourfile The first command removes all files from the
directory; the second removes the empty directory.
If your neighbor doesn’t want you poking You can go up one level in the tree of files by
around in his files, or vice versa, privacy can be saying
arranged. Each file and directory has read-write-
execute permissions for the owner, a group, and cd ..
everyone else, which can be set to control access.
−− −−
-8-
‘‘..’’ is the name of the parent of whatever direc- new page. Suppose you want them run together
tory you are currently in. For completeness, ‘‘.’’ is instead. You could say
an alternate name for the directory you are in.
cat f g h >temp
pr <temp
Using Files instead of the Terminal
rm temp
Most of the commands we have seen so far
produce output on the terminal; some, like the edi- but this is more work than necessary. Clearly what
tor, also take their input from the terminal. It is we want is to take the output of cat and connect it
universal in UNIX systems that the terminal can be to the input of pr. So let us use a pipe:
replaced by a file for either or both of input and
cat f g h | pr
output. As one example,
The vertical bar | means to take the output from
ls
cat, which would normally have gone to the termi-
makes a list of files on your terminal. But if you nal, and put it into pr to be neatly formatted.
say There are many other examples of pipes. For
example,
ls >filelist
ls | pr −3
a list of your files will be placed in the file filelist
(which will be created if it doesn’t already exist, or prints a list of your files in three columns. The
overwritten if it does). The symbol > means ‘‘put program wc counts the number of lines, words and
the output on the following file, rather than on the characters in its input, and as we saw earlier, who
terminal.’’ Nothing is produced on the terminal. prints a list of currently-logged on people, one per
As another example, you could combine several line. Thus
files into one by capturing the output of cat in a
who | wc
file:
tells how many people are logged on. And of
cat f1 f2 f3 >temp
course
The symbol >> operates very much like >
ls | wc
does, except that it means ‘‘add to the end of.’’
That is, counts your files.
Any program that reads from the terminal can
cat f1 f2 f3 >>temp
read from a pipe instead; any program that writes
means to concatenate f1, f2 and f3 to the end of on the terminal can drive a pipe. You can have as
whatever is already in temp, instead of overwriting many elements in a pipeline as you wish.
the existing contents. As with >, if temp doesn’t Many UNIX programs are written so that they
exist, it will be created for you. will take their input from one or more files if file
In a similar way, the symbol < means to take arguments are given; if no arguments are given
the input for a program from the following file, they will read from the terminal, and thus can be
instead of from the terminal. Thus, you could used in pipelines. pr is one example:
make up a script of commonly used editing com-
pr −3 a b c
mands and put them into a file called script. Then
you can run the script on a file by saying prints files a, b and c in order in three columns.
But in
ed file <script
cat a b c | pr −3
As another example, you can use ed to prepare a
letter in file let, then send it to several people with pr prints the information coming down the
pipeline, still in three columns.
mail adam eve mary joe <let
The Shell
Pipes We have already mentioned once or twice the
One of the novel contributions of the UNIX mysterious ‘‘shell,’’ which is in fact sh(1). The
system is the idea of a pipe. A pipe is simply a shell is the program that interprets what you type
way to connect the output of one program to the as commands and arguments. It also looks after
input of another program, so the two run as a translating *, etc., into lists of filenames, and <, >,
sequence of processes — a pipeline. and | into changes of input and output streams.
For example, The shell has other capabilities too. For
example, you can run two programs with one com-
pr f g h
mand line by separating the commands with a
will print the files f, g, and h, beginning each on a semicolon; the shell recognizes the semicolon and
-- --
-9-
breaks the line into two commands. Thus If this is to be a regular thing, you can elimi-
nate the need to type sh: simply type, once only,
date; who
the command
does both commands before returning with a
chmod +x startup
prompt character.
You can also have more than one program and thereafter you need only say
running simultaneously if you wish. For example,
startup
if you are doing something time-consuming, like
the editor script of an earlier section, and you don’t to run the sequence of commands. The chmod(1)
want to wait around for the results before starting command marks the file executable; the shell rec-
something else, you can say ognizes this and runs it as a sequence of com-
mands.
ed file <script &
If you want startup to run automatically
The ampersand at the end of a command line says every time you log in, create a file in your login
‘‘start this command running, then take further directory called .profile, and place in it the line
commands from the terminal immediately,’’ that is, startup. When the shell first gains control when
don’t wait for it to complete. Thus the script will you log in, it looks for the .profile file and does
begin, but you can do something else at the same whatever commands it finds in it. We’ll get back
time. Of course, to keep the output from interfer- to the shell in the section on programming.
ing with what you’re doing on the terminal, it
would be better to say
III. DOCUMENT PREPARATION
ed file <script >script.out &
UNIX systems are used extensively for docu-
which saves the output lines in a file called ment preparation. There are two major formatting
script.out. programs, that is, programs that produce a text
When you initiate a command with &, the with justified right margins, automatic page num-
system replies with a number called the process bering and titling, automatic hyphenation, and the
number, which identifies the command in case you like. nroff is designed to produce output on termi-
later want to stop it. If you do, you can say nals and line-printers. troff (pronounced ‘‘tee-
roff’’) instead drives a phototypesetter, which pro-
kill process-number
duces very high quality output on photographic
If you forget the process number, the command ps paper. This paper was formatted with troff.
will tell you about everything you have running.
(If you are desperate, kill 0 will kill all your pro- Formatting Packages
cesses.) And if you’re curious about other people, The basic idea of nroff and troff is that the
ps a will tell you about all programs that are cur- text to be formatted contains within it ‘‘formatting
rently running. commands’’ that indicate in detail how the format-
You can say ted text is to look. For example, there might be
commands that specify how long lines are, whether
(command-1; command-2; command-3) &
to use single or double spacing, and what running
to start three commands in the background, or you titles to use on each page.
can start a background pipeline with Because nroff and troff are relatively hard to
learn to use effectively, several ‘‘packages’’ of
command-1 | command-2 &
canned formatting requests are available to let you
Just as you can tell the editor or some similar specify paragraphs, running titles, footnotes, multi-
program to take its input from a file instead of column output, and so on, with little effort and
from the terminal, you can tell the shell to read a without having to learn nroff and troff. These
file to get commands. (Why not? The shell, after packages take a modest effort to learn, but the
all, is just a program, albeit a clever one.) For rewards for using them are so great that it is time
instance, suppose you want to set tabs on your ter- well spent.
minal, and find out the date and who’s on the sys- In this section, we will provide a hasty look at
tem every time you log in. Then you can put the the ‘‘manuscript’’ package known as −ms. For-
three necessary commands (tabs, date, who) into a matting requests typically consist of a period and
file, let’s call it startup, and then run it with two upper-case letters, such as .TL, which is used
to introduce a title, or .PP to begin a new para-
sh startup
graph.
This says to run the shell with the file startup as A document is typed so it looks something
input. The effect is as if you had typed the con- like this:
tents of startup on the terminal.
.TL
−− −−
- 10 -
- 11 -
- 12 -
- 13 -
The Bell System Technical Journal (BSTJ) Special S. I. Feldman, ‘‘MAKE — A Program for Main-
Issue on UNIX, July/August, 1978, contains many taining Computer Programs,’’ Bell Laboratories
papers describing recent developments, and some CSTR 57, 1977.
retrospective material. J. F. Maranzano and S. R. Bourne, ‘‘A Tutorial
The 2nd International Conference on Software Introduction to ADB,’’ Bell Laboratories CSTR
Engineering (October, 1976) contains several 62, 1977. An introduction to a powerful but com-
papers describing the use of the Programmer’s plex debugging tool.
Workbench (PWB) version of UNIX. S. I. Feldman and P. J. Weinberger, ‘‘A Portable
Fortran 77 Compiler,’’ Bell Laboratories, 1978. A
Document Preparation: full Fortran 77 for UNIX systems.
B. W. Kernighan, ‘‘A Tutorial Introduction to the
UNIX Text Editor’’ and ‘‘Advanced Editing on
UNIX,’’ Bell Laboratories, 1978. Beginners need
the introduction; the advanced material will help
you get the most out of the editor.
M. E. Lesk, ‘‘Typing Documents on UNIX,’’ Bell
Laboratories, 1978. Describes the −ms macro
package, which isolates the novice from the
vagaries of nroff and troff, and takes care of most
formatting situations. If this specific package isn’t
available on your system, something similar proba-
bly is. The most likely alternative is the
PWB/UNIX macro package −mm; see your local
guru if you use PWB/UNIX.
B. W. Kernighan and L. L. Cherry, ‘‘A System for
Typesetting Mathematics,’’ Bell Laboratories
Computing Science Tech. Rep. 17.
M. E. Lesk, ‘‘Tbl — A Program to Format
Tables,’’ Bell Laboratories CSTR 49, 1976.
J. F. Ossanna, Jr., ‘‘NROFF/TROFF User’s Man-
ual,’’ Bell Laboratories CSTR 54, 1976. troff is
the basic formatter used by −ms, eqn and tbl. The
reference manual is indispensable if you are going
to write or maintain these or similar programs.
But start with:
B. W. Kernighan, ‘‘A TROFF Tutorial,’’ Bell Labo-
ratories, 1976. An attempt to unravel the intrica-
cies of troff.
Programming:
B. W. Kernighan and D. M. Ritchie, The C Pro-
gramming Language, Prentice-Hall, 1978. Con-
tains a tutorial introduction, complete discussions
of all language features, and the reference manual.
B. W. Kernighan and D. M. Ritchie, ‘‘UNIX Pro-
gramming,’’ Bell Laboratories, 1978. Describes
how to interface with the system from C programs:
I/O calls, signals, processes.
S. R. Bourne, ‘‘An Introduction to the UNIX
Shell,’’ Bell Laboratories, 1978. An introduction
and reference manual for the Version 7 shell.
Mandatory reading if you intend to make effective
use of the programming power of this shell.
S. C. Johnson, ‘‘Yacc — Yet Another Compiler-
Compiler,’’ Bell Laboratories CSTR 32, 1978.
M. E. Lesk, ‘‘Lex — A Lexical Analyzer Genera-
tor,’’ Bell Laboratories CSTR 39, 1975.
S. C. Johnson, ‘‘Lint, a C Program Checker,’’ Bell
Laboratories CSTR 65, 1977.