UNIX Programmer's Manual
UNIX Programmer's Manual
January, 1979
Seventh Edition
January 10, 1979
This volume contains documents which supplement the information contained in Volume 1 of The
UNIX† Programmer’s Manual. The documents here are grouped roughly into the areas of basics, editing,
language tools, document preparation, and system maintenance. Further general information may be
found in the Bell System Technical Journal special issue on UNIX, July-August, 1978.
Many of the documents cited within this volume as Bell Laboratories internal memoranda or Com-
puting Science Technical Reports (CSTR) are also contained here.
These documents contain occasional localisms, typically references to other operating systems like
GCOS and IBM. In all cases, such references may be safely ignored by UNIX users.
General Works
1. 7th Edition UNIX — Summary.
A concise summary of the facilities available on UNIX.
2. The UNIX Time-Sharing System. D. M. Ritchie and K. Thompson.
The original UNIX paper, reprinted from CACM.
Getting Started
3. UNIX for Beginners — Second Edition. B. W. Kernighan.
An introduction to the most basic use of the system.
4. A Tutorial Introduction to the UNIX Text Editor. B. W. Kernighan.
An easy way to get started with the editor.
5. Advanced Editing on UNIX. B. W. Kernighan.
The next step.
6. An Introduction to the UNIX Shell. S. R. Bourne.
An introduction to the capabilities of the command interpreter, the shell.
7. Learn — Computer Aided Instruction on UNIX. M. E. Lesk and B. W. Kernighan.
Describes a computer-aided instruction program that walks new users through the basics of
files, the editor, and document preparation software.
Document Preparation
8. Typing Documents on the UNIX System. M. E. Lesk.
Describes the basic use of the formatting tools. Also describes ‘‘– ms’’, a standardized
package of formatting requests that can be used to lay out most documents (including those
in this volume).
__________________
†UNIX is a Trademark of Bell Laboratories.
-2-
Programming
14. The C Programming Language — Reference Manual. D. M. Ritchie.
Official statement of the syntax and semantics of C. Should be supplemented by The C
Programming Language, B. W. Kernighan and D. M. Ritchie, Prentice-Hall, 1978, which
contains a tutorial introduction and many examples.
15. Lint, A C Program Checker. S. C. Johnson.
Checks C programs for syntax errors, type violations, portability problems, and a variety of
probable errors.
16. Make — A Program for Maintaining Computer Programs. S. I. Feldman.
Indispensable tool for making sure that large programs are properly compiled with minimal
effort.
17. UNIX Programming. B. W. Kernighan and D. M. Ritchie.
Describes the programming interface to the operating system and the standard I/O library.
18. A Tutorial Introduction to ADB. J. F. Maranzano and S. R. Bourne.
How to use the ADB debugger.
September 6, 1978
Bell Laboratories
Murray Hill, New Jersey 07974
__________________
† UNIX is a Trademark of Bell Laboratories.
-2-
B. Hardware
The 7th edition UNIX operating system runs on a DEC PDP-11/45 or 11/70* with at least the fol-
lowing equipment:
128K to 2M words of managed memory; parity not used.
disk: RP03, RP04, RP06, RK05 (more than 1 RK05) or equivalent.
console typewriter.
clock: KW11-L or KW11-P.
The following equipment is strongly recommended:
communications controller such as DL11 or DH11.
full duplex 96-character ASCII terminals.
9-track tape or extra disk for system backup.
The system is normally distributed on 9-track tape. The minimum memory and disk space specified is
enough to run and maintain UNIX. More will be needed to keep all source on line, or to handle a large
number of users, big data bases, diversified complements of devices, or large programs. The resident
code occupies 12-20K words depending on configuration; system data occupies 10-28K words.
There is no commitment to provide 7th edition UNIX on PDP-11/34, 11/40 and 11/60 hardware.
C. Software
Most of the programs available as UNIX commands are listed. Source code and printed manuals
are distributed for all of the listed software except games. Almost all of the code is written in C. Com-
mands are self-contained and do not require extra setup information, unless specifically noted as
‘‘interactive.’’ Interactive programs can be made to run from a prepared script simply by redirecting
input. Most programs intended for interactive use (e.g., the editor) allow for an escape to command
level (the Shell). Most file processing commands can also go from standard input to standard output
(‘‘filters’’). The piping facility of the Shell may be used to connect such filters directly to the input or
output of other programs.
1. Basic Software
This includes the time-sharing operating system with utilities, a machine language assembler and a
compiler for the programming language C—enough software to write and run new applications and to
maintain or modify UNIX itself.
__________________
*PDP is a Trademark of Digital Equipment Corporation.
-3-
CAT Concatenate one or more files onto standard output. Particularly used for unadorned
printing, for inserting data into a pipeline, and for buffering output that comes in dribs
and drabs. Works on any file regardless of contents.
CP Copy one file to another, or a set of files to a directory. Works on any file regardless
of contents.
PR Print files with title, date, and page number on every page.
Multicolumn output.
Parallel column merge of several files.
LPR Off-line print. Spools arbitrary files to the line printer.
CMP Compare two files and report if different.
TAIL Print last n lines of input
May print last n characters, or from n lines or characters to end.
SPLIT Split a large file into more manageable pieces. Occasionally necessary for editing
(ED).
DD Physical file format translator, for exchanging data with foreign systems, especially
IBM 370’s.
SUM Sum the words of a file.
RM Remove a file. Only the name goes away if any other names are linked to the file.
Step through a directory deleting files interactively.
Delete entire directory hierarchies.
LN ‘‘Link’’ another name (alias) to an existing file.
MV Move a file or files. Used for renaming files.
CHMOD Change permissions on one or more files. Executable by files’ owner.
CHOWN Change owner of one or more files.
CHGRP Change group (project) to which a file belongs.
MKDIR Make a new directory.
RMDIR Remove a directory.
CD Change working directory.
FIND Prowl the directory hierarchy finding every file that meets specified criteria.
Criteria include:
name matches a given pattern,
creation date in given range,
date of last use in given range,
given permissions,
given owner,
given special file characteristics,
boolean combinations of above.
-5-
LS List the names of one, several, or all files in one or more directories.
Alphabetic or temporal sorting, up or down.
Optional information: size, owner, group, date last modified, date last accessed, per-
missions, i-node number.
FILE Try to determine what kind of information is in a file by consulting the file system
index and by reading the file itself.
DATE Print today’s date and time. Has considerable knowledge of calendric and horological
peculiarities.
May set UNIX’s idea of date and time.
DF Report amount of free space on file system devices.
DU Print a summary of total space occupied by all files in a hierarchy.
QUOT Print summary of file space usage by user id.
WHO Tell who’s on the system.
List of presently logged in users, ports and times on.
Optional history of all logins and logouts.
PS Report on active processes.
List your own or everybody’s processes.
Tell what commands are being executed.
Optional status information: state and scheduling info, priority, attached terminal,
what it’s waiting for, size.
IOSTAT Print statistics about system I/O activity.
TTY Print name of your terminal.
PWD Print name of your working directory.
MOUNT Attach a device containing a file system to the tree of directories. Protects against
nonsense arrangements.
UMOUNT Remove the file system contained on a device from the tree of directories. Protects
against removing a busy device.
MKFS Make a new file system on a device.
MKNOD Make an i-node (file system entry) for a special file. Special files are physical devices,
virtual devices, physical memory, etc.
TP
TAR Manage file archives on magnetic tape or DECtape. TAR is newer.
Collect files into an archive.
Update DECtape archive by date.
Replace or delete DECtape files.
Print table of contents.
Retrieve from archive.
DUMP Dump the file system stored on a specified device, selectively by date, or indiscrim-
inately.
-7-
1.9. Accounting
The timing information on which the reports are based can be manually cleared or shut off completely.
AC Publish cumulative connect time report.
Connect time by user or by day.
For all users or for selected users.
SA Publish Shell accounting report. Gives usage information on each command executed.
Number of times used.
Total system time, user time and elapsed time.
Optional averages and percentages.
Sorting on various fields.
1.10. Communication
MAIL Mail a message to one or more users. Also used to read and dispose of incoming
mail. The presence of mail is announced by LOGIN and optionally by SH.
Each message can be disposed of individually.
Messages can be saved in files or forwarded.
CALENDAR Automatic reminder service for events of today and tomorrow.
WRITE Establish direct terminal communication with another user.
WALL Write to all users.
MESG Inhibit receipt of messages from WRITE and WALL.
CU Call up another time-sharing system.
Transparent interface to remote machine.
File transmission.
Take remote input from local file or put remote output into local file.
Remote system need not be UNIX.
UUCP UNIX to UNIX copy.
-8-
Automatic queuing until line becomes available and remote machine is up.
Copy between two remote machines.
Differences, mail, etc., between two machines.
LEARN A program for interpreting CAI scripts, plus scripts for learning about UNIX by using
it.
Scripts for basic files and commands, editor, advanced files and commands, EQN,
MS macros, C programming language.
2. Languages
CC Compile and/or link edit programs in the C language. The UNIX operating system,
most of the subsystems and C itself are written in C. For a full description of C, read
The C Programming Language, Brian W. Kernighan and Dennis M. Ritchie, Prentice-
Hall, 1978.
General purpose language designed for structured programming.
Data types include character, integer, float, double, pointers to all types, functions
returning above types, arrays of all types, structures and unions of all types.
Operations intended to give machine-independent control of full machine facility,
including to-memory operations and pointer arithmetic.
Macro preprocessor for parameterized code and inclusion of standard files.
All procedures recursive, with parameters by value.
Machine-independent pointer manipulation.
Object code uses full addressing capability of the PDP-11.
Runtime library gives access to all system facilities.
Definable data types.
Block structure
LINT Verifier for C programs. Reports questionable or nonportable usage such as:
Mismatched data declarations and procedure interfaces.
Nonportable type conversions.
Unused variables, unreachable code, no-effect operations.
Mistyped pointers.
Obsolete syntax.
Full cross-module checking of separately compiled programs.
CB A beautifier for C programs. Does proper indentation and placement of braces.
2.2. Fortran
Statements include:
comment,
dump,
for...next,
goto,
if...else...fi,
list,
print,
prompt,
return,
run,
save.
All calculations double precision.
Recursive function defining and calling.
Builtin functions include log, exp, sin, cos, atn, int, sqr, abs, rnd.
Escape to ED for complex program editing.
DC Interactive programmable desk calculator. Has named storage locations as well as con-
ventional stack for holding integers or programs.
Unlimited precision decimal arithmetic.
Appropriate treatment of decimal fractions.
Arbitrary input and output radices, in particular binary, octal, decimal and hexade-
cimal.
Reverse Polish operators:
+– */
remainder, power, square root,
load, store, duplicate, clear,
print, enter program text, execute.
BC A C-like interactive interface to the desk calculator DC.
All the capabilities of DC with a high-level syntax.
Arrays and recursive functions.
Immediate evaluation of expressions and evaluation of functions upon call.
Arbitrary precision elementary functions: exp, sin, cos, atan.
Go-to-less programming.
2.4. Macroprocessing
2.5. Compiler-compilers
YACC An LR(1)-based compiler writing system. During execution of resulting parsers, arbi-
trary C functions may be called to do code generation or semantic actions.
BNF syntax specifications.
Precedence relations.
Accepts formally ambiguous grammars with non-BNF resolution rules.
LEX Generator of lexical analyzers. Arbitrary C functions may be called upon isolation of
each lexical token.
- 12 -
3. Text Processing
ROFF A typesetting program for terminals. Easy for nontechnical people to learn, and good
for simple documents. Input consists of data lines intermixed with control lines, such
as
4. Information Handling
5. Graphics
The programs in this section are predominantly intended for use with Tektronix 4014 storage scopes.
GRAPH Prepares a graph of a set of input numbers.
Input scaled to fit standard plotting area.
Abscissae may be supplied automatically.
Graph may be labeled.
Control over grid style, line style, graph orientation, etc.
SPLINE Provides a smooth curve through a set of points intended for GRAPH.
PLOT A set of filters for printing graphs produced by GRAPH and other programs on various
terminals. Filters provided for 4014, DASI terminals, Versatec printer/plotter.
BACKGAMMON
A player of modest accomplishment.
CHESS Plays good class D chess.
- 16 -
ABSTRACT
1. INTRODUCTION
There have been four versions of the UNIX time-sharing system. The earliest (circa 1969-70) ran
on the Digital Equipment Corporation PDP-7 and -9 computers. The second version ran on the unpro-
tected PDP-11/20 computer. The third incorporated multiprogramming and ran on the PDP-11/34, /40,
/45, /60, and /70 computers; it is the one described in the previously published version of this paper, and
is also the most widely used today. This paper describes only the fourth, current system that runs on the
PDP-11/70 and the Interdata 8/32 computers. In fact, the differences among the various systems is rather
small; most of the revisions made to the originally published version of this paper, aside from those con-
cerned with style, had to do with details of the implementation of the file system.
Since PDP-11 UNIX became operational in February, 1971, over 600 installations have been put into
service. Most of them are engaged in applications such as computer science education, the preparation
and formatting of documents and other textual material, the collection and processing of trouble data
from various switching machines within the Bell System, and recording and checking telephone service
orders. Our own installation is used mainly for research in operating systems, languages, computer net-
works, and other topics in computer science, and also for document preparation.
Perhaps the most important achievement of UNIX is to demonstrate that a powerful operating sys-
tem for interactive use need not be expensive either in equipment or in human effort: it can run on
hardware costing as little as $40,000, and less than two man-years were spent on the main system
software. We hope, however, that users find that the most important characteristics of the system are its
simplicity, elegance, and ease of use.
Besides the operating system proper, some major programs available under UNIX are
__________________
* Copyright 1974, Association for Computing Machinery, Inc., reprinted by permission. This is a revised version of an
article that appeared in Communications of the ACM, 17, No. 7 (July 1974), pp. 365-375. That article was a revised
version of a paper presented at the Fourth ACM Symposium on Operating Systems Principles, IBM Thomas J. Watson
Research Center, Yorktown Heights, New York, October 15-17, 1973.
†UNIX is a Trademark of Bell Laboratories.
-2-
C compiler
Text editor based on QED1
Assembler, linking loader, symbolic debugger
Phototypesetting and equation setting programs23
Dozens of languages including Fortran 77, Basic, Snobol, APL, Algol 68, M6, TMG, Pascal
There is a host of maintenance, utility, recreation and novelty programs, all written locally. The UNIX
user community, which numbers in the thousands, has contributed many more programs and languages.
It is worth noting that the system is totally self-supporting. All UNIX software is maintained on the sys-
tem; likewise, this paper and all other documents in this issue were generated and formatted by the UNIX
editor and text formatting programs.
3.2 Directories
Directories provide the mapping between the names of files and the files themselves, and thus
induce a structure on the file system as a whole. Each user has a directory of his own files; he may also
create subdirectories to contain groups of files conveniently treated together. A directory behaves
exactly like an ordinary file except that it cannot be written on by unprivileged programs, so that the
system controls the contents of directories. However, anyone with appropriate permission may read a
directory just like any other file.
-3-
The system maintains several directories for its own use. One of these is the root directory. All
files in the system can be found by tracing a path through a chain of directories until the desired file is
reached. The starting point for such searches is often the root. Other system directories contain all the
programs provided for general use; that is, all the commands. As will be seen, however, it is by no
means necessary that a program reside in one of these directories for it to be executed.
Files are named by sequences of 14 or fewer characters. When the name of a file is specified to
the system, it may be in the form of a path name, which is a sequence of directory names separated by
slashes, ‘‘/ ’’, and ending in a file name. If the sequence begins with a slash, the search begins in the
root directory. The name /alpha/beta/gamma causes the system to search the root for directory alpha,
then to search alpha for beta, finally to find gamma in beta. gamma may be an ordinary file, a direc-
tory, or a special file. As a limiting case, the name ‘‘/ ’’ refers to the root itself.
A path name not starting with ‘‘/ ’’ causes the system to begin the search in the user’s current
directory. Thus, the name alpha/beta specifies the file named beta in subdirectory alpha of the current
directory. The simplest kind of name, for example, alpha, refers to a file that itself is found in the
current directory. As another limiting case, the null file name refers to the current directory.
The same non-directory file may appear in several directories under possibly different names.
This feature is called linking; a directory entry for a file is sometimes called a link. The UNIX system
differs from other systems in which linking is permitted in that all links to a file have equal status. That
is, a file does not exist within a particular directory; the directory entry for a file consists merely of its
name and a pointer to the information actually describing the file. Thus a file exists independently of
any directory entry, although in practice a file is made to disappear along with the last link to it.
Each directory always has at least two entries. The name ‘‘ . ’’ in each directory refers to the
directory itself. Thus a program may read the current directory under the name ‘‘ . ’’ without knowing
its complete path name. The name ‘‘ . . ’’ by convention refers to the parent of the directory in which it
appears, that is, to the directory in which it was created.
The directory structure is constrained to have the form of a rooted tree. Except for the special
entries ‘‘ . ’’ and ‘‘ . . ’’, each directory must appear as an entry in exactly one other directory, which is
its parent. The reason for this is to simplify the writing of programs that visit subtrees of the directory
structure, and more important, to avoid the separation of portions of the hierarchy. If arbitrary links to
directories were permitted, it would be quite difficult to detect when the last connection from the root to
a directory was severed.
leaf of the hierarchy tree (the ordinary file) by a whole new subtree (the hierarchy stored on the remov-
able volume). After the mount, there is virtually no distinction between files on the removable volume
and those in the permanent file system. In our installation, for example, the root directory resides on a
small partition of one of our disk drives, while the other drive, which contains the user’s files, is
mounted by the system initialization sequence. A mountable file system is generated by writing on its
corresponding special file. A utility program is available to create an empty file system, or one may
simply copy an existing file system.
There is only one exception to the rule of identical treatment of files on different devices: no link
may exist between one file system hierarchy and another. This restriction is enforced so as to avoid the
elaborate bookkeeping that would otherwise be required to assure removal of the links whenever the
removable volume is dismounted.
3.5 Protection
Although the access control scheme is quite simple, it has some unusual features. Each user of
the system is assigned a unique user identification number. When a file is created, it is marked with the
user ID of its owner. Also given for new files is a set of ten protection bits. Nine of these specify
independently read, write, and execute permission for the owner of the file, for other members of his
group, and for all remaining users.
If the tenth bit is on, the system will temporarily change the user identification (hereafter, user ID)
of the current user to that of the creator of the file whenever the file is executed as a program. This
change in user ID is effective only during the execution of the program that calls for it. The set-user-ID
feature provides for privileged programs that may use files inaccessible to other users. For example, a
program may keep an accounting file that should neither be read nor changed except by the program
itself. If the set-user-ID bit is on for the program, it may access the file although this access might be
forbidden to other programs invoked by the given program’s user. Since the actual user ID of the
invoker of any program is always available, set-user-ID programs may take any measures desired to
satisfy themselves as to their invoker’s credentials. This mechanism is used to allow users to execute
the carefully written commands that call privileged system entries. For example, there is a system entry
invokable only by the ‘‘super-user’’ (below) that creates an empty directory. As indicated above, direc-
tories are expected to have entries for ‘‘ . ’’ and ‘‘ . . ’’. The command which creates a directory is
owned by the super-user and has the set-user-ID bit set. After it checks its invoker’s authorization to
create the specified directory, it creates it and makes the entries for ‘‘ . ’’ and ‘‘ . . ’’.
Because anyone may set the set-user-ID bit on one of his own files, this mechanism is generally
available without administrative intervention. For example, this protection scheme easily solves the MOO
accounting problem posed by ‘‘Aleph-null.’’8
The system recognizes one particular user ID (that of the ‘‘super-user’’) as exempt from the usual
constraints on file access; thus (for example), programs may be written to dump and reload the file sys-
tem without unwanted interference from the protection system.
indicates whether the file is to be read, written, or ‘‘updated,’’ that is, read and written simultaneously.
The returned value filep is called a file descriptor. It is a small integer used to identify the file in
subsequent calls to read, write, or otherwise manipulate the file.
To create a new file or completely rewrite an old one, there is a create system call that creates the
given file if it does not exist, or truncates it to zero length if it does exist; create also opens the new file
for writing and, like open, returns a file descriptor.
The file system maintains no locks visible to the user, nor is there any restriction on the number
of users who may have a file open for reading or writing. Although it is possible for the contents of a
file to become scrambled when two users write on it simultaneously, in practice difficulties do not arise.
We take the view that locks are neither necessary nor sufficient, in our environment, to prevent interfer-
ence between users of the same file. They are unnecessary because we are not faced with large, single-
file data bases maintained by independent processes. They are insufficient because locks in the ordinary
sense, whereby one user is prevented from writing on a file that another user is reading, cannot prevent
confusion when, for example, both users are editing a file with an editor that makes a copy of the file
being edited.
There are, however, sufficient internal interlocks to maintain the logical consistency of the file sys-
tem when two users engage simultaneously in activities such as writing on the same file, creating files in
the same directory, or deleting each other’s open files.
Except as indicated below, reading and writing are sequential. This means that if a particular byte
in the file was the last byte written (or read), the next I/O call implicitly refers to the immediately fol-
lowing byte. For each open file there is a pointer, maintained inside the system, that indicates the next
byte to be read or written. If n bytes are read or written, the pointer advances by n bytes.
Once a file is open, the following calls may be used:
n = read ( filep, buffer, count )
n = write ( filep, buffer, count )
Up to count bytes are transmitted between the file specified by filep and the byte array specified by
buffer. The returned value n is the number of bytes actually transmitted. In the write case, n is the
same as count except under exceptional conditions, such as I/O errors or end of physical medium on
special files; in a read, however, n may without error be less than count. If the read pointer is so near
the end of the file that reading count characters would cause reading beyond the end, only sufficient
bytes are transmitted to reach the end of the file; also, typewriter-like terminals never return more than
one line of input. When a read call returns with n equal to zero, the end of the file has been reached.
For disk files this occurs when the read pointer becomes equal to the current size of the file. It is possi-
ble to generate an end-of-file from a terminal by use of an escape sequence that depends on the device
used.
Bytes written affect only those parts of a file implied by the position of the write pointer and the
count; no other part of the file is changed. If the last byte lies beyond the end of the file, the file is
made to grow as needed.
To do random (direct-access) I/O it is only necessary to move the read or write pointer to the
appropriate location in the file.
location = lseek ( filep, offset, base )
The pointer associated with filep is moved to a position offset bytes from the beginning of the file, from
the current position of the pointer, or from the end of the file, depending on base. offset may be nega-
tive. For some devices (e.g., paper tape and terminals) seek calls are ignored. The actual offset from
the beginning of the file to which the pointer was moved is returned in location.
There are several additional system entries having to do with I/O and with the file system that will
not be discussed. For example: close a file, get the status of a file, change the protection mode or the
owner of a file, create a directory, make a link to an existing file, delete a file.
-6-
can be read with a single disk access; bytes in the range 5120 to 70,656 require two accesses; bytes in
the range 70,656 to 8,459,264 require three accesses; bytes from there to the largest file (1,082,201,088)
require four accesses. In practice, a device cache mechanism (see below) proves effective in eliminating
most of the indirect fetches.
The foregoing discussion applies to ordinary files. When an I/O request is made to a file whose
i-node indicates that it is special, the last 12 device address words are immaterial, and the first specifies
an internal device name, which is interpreted as a pair of numbers representing, respectively, a device
type and subdevice number. The device type indicates which system routine will deal with I/O on that
device; the subdevice number selects, for example, a disk drive attached to a particular controller or one
of several similar terminal interfaces.
In this environment, the implementation of the mount system call (Section 3.4) is quite straight-
forward. mount maintains a system table whose argument is the i-number and device name of the ordi-
nary file specified during the mount, and whose corresponding value is the device name of the indicated
special file. This table is searched for each i-number/device pair that turns up while a path name is
being scanned during an open or create; if a match is found, the i-number is replaced by the i-number
of the root directory and the device name is replaced by the table value.
To the user, both reading and writing of files appear to be synchronous and unbuffered. That is,
immediately after return from a read call the data are available; conversely, after a write the user’s
-7-
workspace may be reused. In fact, the system maintains a rather complicated buffering mechanism that
reduces greatly the number of I/O operations required to access a file. Suppose a write call is made
specifying transmission of a single byte. The system will search its buffers to see whether the affected
disk block currently resides in main memory; if not, it will be read in from the device. Then the
affected byte is replaced in the buffer and an entry is made in a list of blocks to be written. The return
from the write call may then take place, although the actual I/O may not be completed until a later time.
Conversely, if a single byte is read, the system determines whether the secondary storage block in which
the byte is located is already in one of the system’s buffers; if so, the byte can be returned immediately.
If not, the block is read into a buffer and the byte picked out.
The system recognizes when a program has made accesses to sequential blocks of a file, and asyn-
chronously pre-reads the next block. This significantly reduces the running time of most programs while
adding little to system overhead.
A program that reads or writes files in units of 512 bytes has an advantage over a program that
reads or writes a single byte at a time, but the gain is not immense; it comes mainly from the avoidance
of system overhead. If a program is used rarely or does no great volume of I/O, it may quite reasonably
read and write in units as small as it wishes.
The notion of the i-list is an unusual feature of UNIX. In practice, this method of organizing the
file system has proved quite reliable and easy to deal with. To the system itself, one of its strengths is
the fact that each file has a short, unambiguous name related in a simple way to the protection, address-
ing, and other information needed to access the file. It also permits a quite simple and rapid algorithm
for checking the consistency of a file system, for example, verification that the portions of each device
containing useful information and those free to be allocated are disjoint and together exhaust the space
on the device. This algorithm is independent of the directory hierarchy, because it need only scan the
linearly organized i-list. At the same time the notion of the i-list induces certain peculiarities not found
in other file system organizations. For example, there is the question of who is to be charged for the
space a file occupies, because all directory entries for a file have equal status. Charging the owner of a
file is unfair in general, for one user may create a file, another may link to it, and the first user may
delete the file. The first user is still the owner of the file, but it should be charged to the second user.
The simplest reasonably fair algorithm seems to be to spread the charges equally among users who have
links to a file. Many installations avoid the issue by not charging any fees at all.
5.1 Processes
Except while the system is bootstrapping itself into operation, a new process can come into
existence only by use of the fork system call:
processid = fork ( )
-8-
When fork is executed, the process splits into two independently executing processes. The two
processes have independent copies of the original memory image, and share all open files. The new
processes differ only in that one is considered the parent process: in the parent, the returned processid
actually identifies the child process and is never 0, while in the child, the returned value is always 0.
Because the values returned by fork in the parent and child process are distinguishable, each pro-
cess may determine whether it is the parent or child.
5.2 Pipes
Processes may communicate with related processes using the same system read and write calls
that are used for file-system I/O. The call:
filep = pipe ( )
returns a file descriptor filep and creates an inter-process channel called a pipe. This channel, like other
open files, is passed from parent to child process in the image by the fork call. A read using a pipe file
descriptor waits until another process writes using the file descriptor for the same pipe. At this point,
data are passed between the images of the two processes. Neither process need know that a pipe, rather
than an ordinary file, is involved.
Although inter-process communication via pipes is a quite valuable tool (see Section 6.2), it is not
a completely general mechanism, because the pipe must be set up by a common ancestor of the
processes involved.
5.5 Termination
Lastly:
exit ( status )
terminates a process, destroys its image, closes its open files, and generally obliterates it. The parent is
notified through the wait primitive, and status is made available to it. Processes may also terminate as
a result of various illegal actions or user-generated signals (Section VII below).
only the theory of its operation.) In simplest form, a command line consists of the command name fol-
lowed by arguments to the command, all separated by spaces:
command arg1 arg2 . . . argn
The shell splits up the command name and the arguments into separate strings. Then a file with name
command is sought; command may be a path name including the ‘‘/’’ character to specify any file in
the system. If command is found, it is brought into memory and executed. The arguments collected by
the shell are accessible to the command. When the command is finished, the shell resumes its own exe-
cution, and indicates its readiness to accept another command by typing a prompt character.
If file command cannot be found, the shell generally prefixes a string such as / bin / to command
and attempts again to find the file. Directory / bin contains commands intended to be generally used.
(The sequence of directories to be searched may be changed by user request.)
6.2 Filters
An extension of the standard I/O notion is used to direct output from one command to the input of
another. A sequence of commands separated by vertical bars causes the shell to execute all the com-
mands simultaneously and to arrange that the standard output of each command be delivered to the stan-
dard input of the next command in the sequence. Thus in the command line:
ls pr −2 opr
ls lists the names of the files in the current directory; its output is passed to pr, which paginates its input
- 10 -
with dated headings. (The argument ‘‘−2’’ requests double-column output.) Likewise, the output from
pr is input to opr; this command spools its input onto a file for off-line printing.
This procedure could have been carried out more clumsily by:
ls >temp1
pr −2 <temp1 >temp2
opr <temp2
followed by removal of the temporary files. In the absence of the ability to redirect output and input, a
still clumsier method would have been to require the ls command to accept user requests to paginate its
output, to print in multi-column format, and to arrange that its output be delivered off-line. Actually it
would be surprising, and in fact unwise for efficiency reasons, to expect authors of commands such as ls
to provide such a wide variety of output options.
A program such as pr which copies its standard input to its standard output (with processing) is
called a filter. Some filters that we have found useful perform character transliteration, selection of lines
according to a pattern, sorting of the input, and encryption and decryption.
6.6 Initialization
The instances of the shell to which users type commands are themselves children of another pro-
cess. The last step in the initialization of the system is the creation of a single process and the invoca-
tion (via execute) of a program called init. The role of init is to create one process for each terminal
channel. The various subinstances of init open the appropriate terminals for input and output on files 0,
1, and 2, waiting, if necessary, for carrier to be established on dial-up lines. Then a message is typed
- 12 -
out requesting that the user log in. When the user types a name or other identification, the appropriate
instance of init wakes up, receives the log-in line, and reads a password file. If the user’s name is
found, and if he is able to supply the correct password, init changes to the user’s default current direc-
tory, sets the process’s user ID to that of the person logging in, and performs an execute of the shell. At
this point, the shell is ready to receive commands and the logging-in protocol is complete.
Meanwhile, the mainstream path of init (the parent of all the subinstances of itself that will later
become shells) does a wait. If one of the child processes terminates, either because a shell found an end
of file or because a user typed an incorrect name or password, this path of init simply recreates the
defunct process, which in turn reopens the appropriate input and output files and types another log-in
message. Thus a user may log out simply by typing the end-of-file sequence to the shell.
VII. TRAPS
The PDP-11 hardware detects a number of program faults, such as references to non-existent
memory, unimplemented instructions, and odd addresses used where an even address is required. Such
faults cause the processor to trap to a system routine. Unless other arrangements have been made, an
illegal action causes the system to terminate the process and to write its image on file core in the current
directory. A debugger can be used to determine the state of the program at the time of the fault.
Programs that are looping, that produce unwanted output, or about which the user has second
thoughts may be halted by the use of the interrupt signal, which is generated by typing the ‘‘delete’’
character. Unless special action has been taken, this signal simply causes the program to cease execu-
tion without producing a core file. There is also a quit signal used to force an image file to be pro-
duced. Thus programs that loop unexpectedly may be halted and the remains inspected without prear-
rangement.
The hardware-generated faults and the interrupt and quit signals can, by request, be either ignored
or caught by a process. For example, the shell ignores quits to prevent a quit from logging the user out.
The editor catches interrupts and returns to its command level. This is useful for stopping long printouts
without losing work in progress (the editor manipulates a copy of the file it is editing). In systems
without floating-point hardware, unimplemented instructions are caught and floating-point instructions
are interpreted.
- 13 -
VIII. PERSPECTIVE
Perhaps paradoxically, the success of the UNIX system is largely due to the fact that it was not
designed to meet any predefined objectives. The first version was written when one of us (Thompson),
dissatisfied with the available computer facilities, discovered a little-used PDP-7 and set out to create a
more hospitable environment. This (essentially personal) effort was sufficiently successful to gain the
interest of the other author and several colleagues, and later to justify the acquisition of the PDP-11/20,
specifically to support a text editing and formatting system. When in turn the 11/20 was outgrown, the
system had proved useful enough to persuade management to invest in the PDP-11/45, and later in the
PDP-11/70 and Interdata 8/32 machines, upon which it developed to its present form. Our goals
throughout the effort, when articulated at all, have always been to build a comfortable relationship with
the machine and to explore ideas and inventions in operating systems and other software. We have not
been faced with the need to satisfy someone else’s requirements, and for this freedom we are grateful.
Three considerations that influenced the design of UNIX are visible in retrospect.
First: because we are programmers, we naturally designed the system to make it easy to write,
test, and run programs. The most important expression of our desire for programming convenience was
that the system was arranged for interactive use, even though the original version only supported one
user. We believe that a properly designed interactive system is much more productive and satisfying to
use than a ‘‘batch’’ system. Moreover, such a system is rather easily adaptable to noninteractive use,
while the converse is not true.
Second: there have always been fairly severe size constraints on the system and its software.
Given the partially antagonistic desires for reasonable efficiency and expressive power, the size con-
straint has encouraged not only economy, but also a certain elegance of design. This may be a thinly
disguised version of the ‘‘salvation through suffering’’ philosophy, but in our case it worked.
Third: nearly from the start, the system was able to, and did, maintain itself. This fact is more
important than it might seem. If designers of a system are forced to use that system, they quickly
become aware of its functional and superficial deficiencies and are strongly motivated to correct them
before it is too late. Because all source programs were always available and easily modified on-line, we
were willing to revise and rewrite the system and its software when new ideas were invented,
discovered, or suggested by others.
The aspects of UNIX discussed in this paper exhibit clearly at least the first two of these design
considerations. The interface to the file system, for example, is extremely convenient from a program-
ming standpoint. The lowest possible interface level is designed to eliminate distinctions between the
various devices and files and between direct and sequential access. No large ‘‘access method’’ routines
are required to insulate the programmer from the system calls; in fact, all user programs either call the
system directly or use a small library program, less than a page long, that buffers a number of characters
and reads or writes them all at once.
Another important aspect of programming convenience is that there are no ‘‘control blocks’’ with
a complicated structure partially maintained by and depended on by the file system or other system calls.
Generally speaking, the contents of a program’s address space are the property of the program, and we
have tried to avoid placing restrictions on the data structures within that address space.
Given the requirement that all programs should be usable with any file or device as input or out-
put, it is also desirable to push device-dependent considerations into the operating system itself. The
only alternatives seem to be to load, with all programs, routines for dealing with each device, which is
expensive in space, or to depend on some means of dynamically linking to the routine appropriate to
each device when it is actually needed, which is expensive either in overhead or in hardware.
Likewise, the process-control scheme and the command interface have proved both convenient and
efficient. Because the shell operates as an ordinary, swappable user program, it consumes no ‘‘wired-
down’’ space in the system proper, and it may be made as powerful as desired at little cost. In particu-
lar, given the framework in which the shell executes as a process that spawns other processes to perform
commands, the notions of I/O redirection, background processes, command files, and user-selectable sys-
tem interfaces all become essentially trivial to implement.
- 14 -
Influences
The success of UNIX lies not so much in new inventions but rather in the full exploitation of a
carefully selected set of fertile ideas, and especially in showing that they can be keys to the implementa-
tion of a small yet powerful operating system.
The fork operation, essentially as we implemented it, was present in the GENIE time-sharing sys-
tem.10 On a number of points we were influenced by Multics, which suggested the particular form of the
I/O system calls11 and both the name of the shell and its general functions. The notion that the shell
should create a process for each command was also suggested to us by the early design of Multics,
although in that system it was later dropped for efficiency reasons. A similar scheme is used by
TENEX.12
IX. STATISTICS
The following numbers are presented to suggest the scale of the Research UNIX operation. Those
of our users not involved in document preparation tend to use the system for program development,
especially language work. There are few important ‘‘applications’’ programs.
Overall, we have today:
There is a ‘‘background’’ process that runs at the lowest possible priority; it is used to soak up any idle
CPU time. It has been used to produce a million-digit approximation to the constant e, and other semi-
infinite problems. Not counting this background work, we average daily:
13,500 commands
9.6 CPU hours
230 connect hours
62 different users
240 log-ins
X. ACKNOWLEDGMENTS
The contributors to UNIX are, in the traditional but here especially apposite phrase, too numerous
to mention. Certainly, collective salutes are due to our colleagues in the Computing Science Research
Center. R. H. Canaday contributed much to the basic design of the file system. We are particularly
appreciative of the inventiveness, thoughtful criticism, and constant support of R. Morris, M. D. McIl-
roy, and J. F. Ossanna.
References
1. L. P. Deutsch and B. W. Lampson, ‘‘An online editor,’’ Comm. Assoc. Comp. Mach. 10(12),
pp.793-799, 803 (December 1967).
2. B. W. Kernighan and L. L. Cherry, ‘‘A System for Typesetting Mathematics,’’ Comm. Assoc.
Comp. Mach. 18, pp.151-157 (March 1975).
3. B. W. Kernighan, M. E. Lesk, and J. F. Ossanna, ‘‘UNIX Time-Sharing System: Document
Preparation,’’ Bell Sys. Tech. J. 57(6), pp.2115-2135 (1978).
- 15 -
4. T. A. Dolotta and J. R. Mashey, ‘‘An Introduction to the Programmer’s Workbench,’’ Proc. 2nd
Int. Conf. on Software Engineering, pp.164-168 (October 13-15, 1976).
5. T. A. Dolotta, R. C. Haight, and J. R. Mashey, ‘‘UNIX Time-Sharing System: The Programmer’s
Workbench,’’ Bell Sys. Tech. J. 57(6), pp.2177-2200 (1978).
6. H. Lycklama, ‘‘UNIX Time-Sharing System: UNIX on a Microprocessor,’’ Bell Sys. Tech. J. 57(6),
pp.2087-2101 (1978).
7. B. W. Kernighan and D. M. Ritchie, The C Programming Language, Prentice-Hall, Englewood
Cliffs, New Jersey (1978).
8. Aleph-null, ‘‘Computer Recreations,’’ Software Practice and Experience 1(2), pp.201-204 (April-
June 1971).
9. S. R. Bourne, ‘‘UNIX Time-Sharing System: The UNIX Shell,’’ Bell Sys. Tech. J. 57(6), pp.1971-
1990 (1978).
10. L. P. Deutsch and B. W. Lampson, ‘‘SDS 930 time-sharing system preliminary reference manual,’’
Doc. 30.10.10, Project GENIE, Univ. Cal. at Berkeley (April 1965).
11. R. J. Feiertag and E. I. Organick, ‘‘The Multics input-output system,’’ Proc. Third Symposium on
Operating Systems Principles, pp.35-41 (October 18-20, 1971).
12. D. G. Bobrow, J. D. Burchfiel, D. L. Murphy, and R. S. Tomlinson, ‘‘TENEX, a Paged Time Shar-
ing System for the PDP-10,’’ Comm. Assoc. Comp. Mach. 15(3), pp.135-143 (March 1972).
UNIX For Beginners — Second Edition
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.
October 2, 1978
_______________
†UNIX is a Trademark of Bell Laboratories.
UNIX For Beginners — Second Edition
Brian W. Kernighan
Bell Laboratories
Murray Hill, New Jersey 07974
The culmination of your login efforts is a and the system will convert each tab into the right
‘‘prompt character,’’ a single character that indicates number of blanks for you. If your terminal does have
that the system is ready to accept commands from computer-settable tabs, the command tabs will set the
you. The prompt character is usually a dollar sign $ stops correctly for you.
or a percent sign %. (You may also get a message
of the day just before the prompt character, or a Mistakes in Typing
notification that you have mail.) If you make a typing mistake, and see it before
RETURN has been typed, there are two ways to
Typing Commands recover. The sharp-character # erases the last charac-
Once you’ve seen the prompt character, you can ter typed; in fact successive uses of # erase characters
type commands, which are requests that the system back to the beginning of the line (but not beyond).
do something. Try typing So if you type badly, you can correct as you go:
date dd#atte##e
followed by RETURN. You should get back some- is the same as date.
thing like The at-sign @ erases all of the characters typed
Mon Jan 16 14:17:10 EST 1978 so far on the current input line, so if the line is irre-
trievably fouled up, type an @ and start the line over.
Don’t forget the RETURN after the command, or noth-
ing will happen. If you think you’re being ignored, What if you must enter a sharp or at-sign as part
type a RETURN; something should happen. RETURN of the text? If you precede either # or @ by a
won’t be mentioned again, but don’t forget it — it backslash \, it loses its erase meaning. So to enter a
has to be there at the end of each line. sharp or at-sign in something, type \# or \@. The
system will always echo a newline at you after your
Another command you might try is who, which at-sign, even if preceded by a backslash. Don’t worry
tells you everyone who is currently logged in: — the at-sign has been recorded.
who To erase a backslash, you have to type two
gives something like sharps or two at-signs, as in \##. The backslash is
used extensively in UNIX to indicate that the follow-
mb tty01 Jan 16 09:11 ing character is in some way special.
ski tty05 Jan 16 09:33
gam tty11 Jan 16 13:07 Read-ahead
The time is when the user logged in; ‘‘ttyxx’’ is the UNIX has full read-ahead, which means that you
system’s idea of what terminal the user is on. can type as fast as you want, whenever you want,
If you make a mistake typing the command even when some command is typing at you. If you
name, and refer to a non-existent command, you will type during output, your input characters will appear
be told. For example, if you type intermixed with the output characters, but they will be
stored away and interpreted in the correct order. So
whom you can type several commands one after another
you will be told without waiting for the first to finish or even begin.
so you’ll be there forever unless you hang up. on yours and vice versa. The path is slow, rather like
talking to the moon. (If you are in the middle of
Mail something, you have to get to a state where you can
When you log in, you may sometimes get the type a command. Normally, whatever program you
message are running has to terminate or be terminated. If
you’re editing, you can escape temporarily from the
You have mail. editor — read the editor tutorial.)
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 getting garbled up with what Joe types. Typically it’s
mail, 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,
Joe now types his message (as many lines as
most recent message first. After each message, mail
he likes). When he’s ready for a reply, he
waits for you to say what to do with it. The two
signals it by typing (o), which stands for
basic responses are d, which deletes the message, and
‘‘over’’.
RETURN, which does not (so it will still be there the
Now Smith types a reply, also terminated by
next time you read your mailbox). Other responses
(o).
are described in the manual. (Earlier versions of mail
This cycle repeats until someone gets tired; he
do not process one message at a time, but are other-
then signals his intent to quit with (oo), for
wise similar.)
‘‘over and out’’.
How do you send mail to someone else? Sup- To terminate the conversation, each side must
pose it is to go to ‘‘joe’’ (assuming ‘‘joe’’ is type a ‘‘control-d’’ character alone on a line.
someone’s login name). The easiest way is this: (‘‘Delete’’ also works.) When the other
mail joe person types his ‘‘control-d’’, you will get the
now type in the text of the letter message EOF on your terminal.
on as many lines as you like ...
If you write to someone who isn’t logged in, or
After the last line of the letter
who doesn’t want to be disturbed, you’ll be told. If
type the character ‘‘control– d’’,
the target is logged in but doesn’t answer after a
that is, hold down ‘‘control’’ and type
decent interval, simply type ‘‘control-d’’.
a letter ‘‘d’’.
And that’s it. The ‘‘control-d’’ sequence, often called On-line Manual
‘‘EOF’’ for end-of-file, is used throughout the system The UNIX Programmer’s Manual is typically
to mark the end of input from a terminal, so you kept on-line. If you get stuck on something, and
might as well get used to it. can’t find an expert to assist you, you can print on
For practice, send mail to yourself. (This isn’t your terminal some manual section that might help.
as strange as it might sound — mail to oneself is a This is also useful for getting the most up-to-date
handy reminder mechanism.) information on a command. To print a manual sec-
There are other ways to send mail — you can tion, type ‘‘man command-name’’. Thus to read up
send a previously prepared letter, and you can mail to on the who command, type
a number of people all at once. For more details see man who
mail(1). (The notation mail(1) means the command
mail in section 1 of the UNIX Programmer’s Manual.) and, of course,
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
Message from joe tty07... Your UNIX system may have available a program
accompanied by a startling beep. It means that Joe called learn, which provides computer aided instruc-
wants to talk to you, but unless you take explicit tion on the file system and basic commands, the edi-
action you won’t be able to talk back. To respond, tor, document preparation, and even C programming.
type the command Try typing the command
This establishes a two-way communication path. If learn exists on your system, it will tell you what to
Now whatever Joe types on his terminal will appear do from there.
-4-
Type a q command to quit the editor. (If you try to Now that you’ve got a file of text, how do you
quit without writing, ed will print a ? to remind you. print it so people can look at it? There are a host of
A second q gets you out regardless.) programs that do that, probably more than are needed.
Now create a second file called temp in the One simple thing is to use the editor, since print-
same manner. You should now have two files, junk ing is often done just before making changes anyway.
and temp. You can say
ed junk
What files are out there? 1,$p
The ls (for ‘‘list’’) command lists the names (not ed will reply with the count of the characters in junk
contents) of any of the files that UNIX knows about. and then print all the lines in the file. After you learn
If you type how to use the editor, you can be selective about the
_____________________ parts you print.
† This is not strictly true — if you hang up while editing, the
There are times when it’s not feasible to use the
data you were working on is saved in a file called ed.hup,
which you can continue with at your next session. editor for printing. For example, there is a limit on
how big a file ed can handle (several thousand lines).
Secondly, it will only print one file at a time, and
-5-
removes all files. (You had better be very sure that’s Although the details will vary according to the
what you wanted to say!) system you are on, if you give the command pwd, it
will print something like
The * is not the only pattern-matching feature
available. Suppose you want to print only chapters 1 /usr/your-name
through 4 and 9. Then you can say This says that you are currently in the directory
pr chap[12349]* your-name, which is in turn in the directory /usr,
which is in turn in the root directory called by con-
The [...] means to match any of the characters inside vention just /. (Even if it’s not called /usr on your
the brackets. A range of consecutive letters or digits system, you will get something analogous. Make the
can be abbreviated, so you can also do this with corresponding changes and read on.)
pr chap[1– 49]* If you now type
Letters can also be used within brackets: [a– z] ls /usr/your-name
matches any character in the range a through z.
you should get exactly the same list of file names as
The ? pattern matches any single character, so you get from a plain ls: with no arguments, ls lists
ls ? the contents of the current directory; given the name
of a directory, it lists the contents of that directory.
lists all files which have single-character names, and
-7-
Next, try
ls /usr/neighbor-name
ls /usr
or make your own copy of one of his files by
This should print a long series of names, among
cp /usr/your-neighbor/his-file yourfile
which is your own login name your-name. On many
systems, usr is a directory that contains the direc- If your neighbor doesn’t want you poking around
tories of all the normal users of the system, like you. in his files, or vice versa, privacy can be arranged.
The next step is to try Each file and directory has read-write-execute permis-
sions for the owner, a group, and everyone else,
ls /
which can be set to control access. See ls(1) and
You should get a response something like this chmod(1) for details. As a matter of observed fact,
(although again the details may be different): most users most of the time find openness of more
benefit than privacy.
bin
dev As a final experiment with pathnames, try
etc ls /bin /usr/bin
lib
tmp Do some of the names look familiar? When you run
usr a program, by typing its name after the prompt char-
acter, the system simply looks for a file of that name.
This is a collection of the basic directories of files It normally looks first in your directory (where it typi-
that the system knows about; we are at the root of the cally doesn’t find it), then in /bin and finally in
tree. /usr/bin. There is nothing magic about commands
Now try like cat or ls, except that they have been collected
into a couple of places to be easy to find and admin-
cat /usr/your-name/junk
ister.
(if junk is still around in your directory). The name What if you work regularly with someone else
/usr/your-name/junk on common information in his directory? You could
just log in as your friend each time you want to, but
is called the pathname of the file that you normally you can also say ‘‘I want to work on his files instead
think of as ‘‘junk’’. ‘‘Pathname’’ has an obvious of my own’’. This is done by changing the directory
meaning: it represents the full name of the path you that you are currently in:
have to follow from the root through the tree of direc-
tories to get to a particular file. It is a universal rule cd /usr/your-friend
in the UNIX system that anywhere you can use an (On some systems, cd is spelled chdir.) Now when
ordinary filename, you can use a pathname. you use a filename in something like cat or pr, it
Here is a picture which may make this clearer: refers to the file in your friend’s directory. Changing
(root) directories doesn’t affect any permissions associated
⁄ \ with a file — if you couldn’t access a file from your
⁄ \ own directory, changing to another directory won’t
⁄ \ alter that fact. Of course, if you forget what directory
bin etc usr dev tmp you’re in, type
⁄ \ ⁄ \ ⁄ \ ⁄ \ ⁄ \
⁄ \ pwd
⁄ \
adam eve mary to find out.
⁄ ⁄ \ \
⁄ \ junk It is usually convenient to arrange your own files
junk temp so that all the files related to one thing are in a direc-
tory separate from other projects. For example, when
Notice that Mary’s junk is unrelated to Eve’s. you write your book, you might want to keep all the
This isn’t too exciting if all the files of interest text in a directory called book. So make one with
are in your own directory, but if you work with mkdir book
someone else or on several projects concurrently, it
becomes handy indeed. For example, your friends then go to it with
can print your book by saying cd book
pr /usr/your-name/chap* then start typing chapters. The book is now found in
Similarly, you can find out what files your neighbor (presumably)
has by saying /usr/your-name/book
-8-
mail adam eve mary joe <let pr prints the information coming down the pipeline,
still in three columns.
-9-
The Shell commands. (Why not? The shell, after all, is just a
We have already mentioned once or twice the program, albeit a clever one.) For instance, suppose
mysterious ‘‘shell,’’ which is in fact sh(1). The shell you want to set tabs on your terminal, and find out
is the program that interprets what you type as com- the date and who’s on the system every time you log
mands and arguments. It also looks after translating in. Then you can put the three necessary commands
*, etc., into lists of filenames, and <, >, and into (tabs, date, who) into a file, let’s call it startup, and
changes of input and output streams. then run it with
ages take a modest effort to learn, but the rewards for that closely resembles the way you would speak it
using them are so great that it is time well spent. aloud. For example, the eqn input
In this section, we will provide a hasty look at sum from i=0 to n x sub i ˜=˜ pi over 2
the ‘‘manuscript’’ package known as – ms. Format-
ting requests typically consist of a period and two produces the output
upper-case letters, such as .TL, which is used to n
π
introduce a title, or .PP to begin a new paragraph. Σ xi
i =0
= __
2
A document is typed so it looks something like
this: The program tbl provides an analogous service
for preparing tabular material; it does all the computa-
.TL tions necessary to align complicated columns with
title of document elements of varying widths.
.AU
refer prepares bibliographic citations from a data
author name
base, in whatever style is defined by the formatting
.SH
package. It looks after all the details of numbering
section heading
references in sequence, filling in page and volume
.PP
numbers, getting the author’s initials and the journal
paragraph ...
name right, and so on.
.PP
another paragraph ... spell and typo detect possible spelling mistakes
.SH in a document. spell works by comparing the words
another section heading in your document to a dictionary, printing those that
.PP are not in the dictionary. It knows enough about
etc. English spelling to detect plurals and the like, so it
does a very good job. typo looks for words which
The lines that begin with a period are the formatting are ‘‘unusual’’, and prints those. Spelling mistakes
requests. For example, .PP calls for starting a new tend to be more unusual, and thus show up early
paragraph. The precise meaning of .PP depends on when the most unusual words are printed first.
what output device is being used (typesetter or termi-
nal, for instance), and on what publication the docu- grep looks through a set of files for lines that
ment will appear in. For example, – ms normally contain a particular text pattern (rather like the
assumes that a paragraph is preceded by a space (one editor’s context search does, but on a bunch of files).
line in nroff, 1⁄2 line in troff), and the first word is For example,
indented. These rules can be changed if you like, but grep ′ing$′ chap*
they are changed by changing the interpretation of
.PP, not by re-typing the document. will find all lines that end with the letters ing in the
files chap*. (It is almost always a good practice to
To actually produce a document in standard for- put single quotes around the pattern you’re searching
mat using – ms, use the command for, in case it contains characters like * or $ that have
troff – ms files ... a special meaning to the shell.) grep is often useful
for finding out in which of a set of files the
for the typesetter, and misspelled words detected by spell are actually
nroff – ms files ... located.
for a terminal. The – ms argument tells troff and diff prints a list of the differences between two
nroff to use the manuscript package of formatting files, so you can compare two versions of something
requests. automatically (which certainly beats proofreading by
hand).
There are several similar packages; check with a
local expert to determine which ones are in common wc counts the words, lines and characters in a
use on your machine. set of files. tr translates characters into other charac-
ters; for example it will convert upper to lower case
Supporting Tools and vice versa. This translates upper into lower:
In addition to the basic formatters, there is a host tr A– Z a– z <input >output
of supporting programs that help with document
preparation. The list in the next few paragraphs is far sort sorts files in a variety of ways; cref makes
from complete, so browse through the manual and cross-references; ptx makes a permuted index
check with people around you for other possibilities. (keyword-in-context listing). sed provides many of
the editing facilities of ed, but can apply them to
eqn and neqn let you integrate mathematics into arbitrarily long inputs. awk provides the ability to do
the text of a document, in an easy-to-learn language both pattern matching and numeric computations, and
- 11 -
building-block programs, you can sometimes avoid mand time will give you the gross run-time statistics
writing a new program merely by piecing together of a program, but they are not super accurate or
some of the building blocks with shell command files. reproducible.
We will not go into any details here; examples
and rules can be found in An Introduction to the UNIX Other Languages
Shell, by S. R. Bourne. If you have to use Fortran, there are two possi-
bilities. You might consider Ratfor, which gives you
Programming in C the decent control structures and free-form input that
If you are undertaking anything substantial, C is characterize C, yet lets you write code that is still
the only reasonable choice of programming language: portable to other environments. Bear in mind that
everything in the UNIX system is tuned to it. The UNIX Fortran tends to produce large and relatively
system itself is written in C, as are most of the pro- slow-running programs. Furthermore, supporting
grams that run on it. It is also a easy language to use software like adb, prof, etc., are all virtually useless
once you get started. C is introduced and fully with Fortran programs. There may also be a Fortran
described in The C Programming Language by B. W. 77 compiler on your system. If so, this is a viable
Kernighan and D. M. Ritchie (Prentice-Hall, 1978). alternative to Ratfor, and has the non-trivial advan-
Several sections of the manual describe the system tage that it is compatible with C and related pro-
interfaces, that is, how you do I/O and similar func- grams. (The Ratfor processor and C tools can be
tions. Read UNIX Programming for more compli- used with Fortran 77 too.)
cated things. If your application requires you to translate a
Most input and output in C is best handled with language into a set of actions or another language,
the standard I/O library, which provides a set of I/O you are in effect building a compiler, though probably
functions that exist in compatible form on most a small one. In that case, you should be using the
machines that have C compilers. In general, it’s yacc compiler-compiler, which helps you develop a
wisest to confine the system interactions in a program compiler quickly. The lex lexical analyzer generator
to the facilities provided by this library. does the same job for the simpler languages that can
be expressed as regular expressions. It can be used
C programs that don’t depend too much on spe- by itself, or as a front end to recognize inputs for a
cial features of UNIX (such as pipes) can be moved to yacc-based program. Both yacc and lex require some
other computers that have C compilers. The list of sophistication to use, but the initial effort of learning
such machines grows daily; in addition to the original them can be repaid many times over in programs that
PDP-11, it currently includes at least Honeywell 6000, are easy to change later on.
IBM 370, Interdata 8/32, Data General Nova and
Eclipse, HP 2100, Harris /7, VAX 11/780, SEL 86, Most UNIX systems also make available other
and Zilog Z80. Calls to the standard I/O library will languages, such as Algol 68, APL, Basic, Lisp, Pas-
work on all of these machines. cal, and Snobol. Whether these are useful depends
largely on the local environment: if someone cares
There are a number of supporting programs that about the language and has worked on it, it may be in
go with C. lint checks C programs for potential por- good shape. If not, the odds are strong that it will be
tability problems, and detects errors such as more trouble than it’s worth.
mismatched argument types and uninitialized vari-
ables. V. UNIX READING LIST
For larger programs (anything whose source is
on more than one file) make allows you to specify General:
the dependencies among the source files and the pro- K. L. Thompson and D. M. Ritchie, The UNIX
cessing steps needed to make a new version; it then Programmer’s Manual, Bell Laboratories, 1978.
checks the times that the pieces were last changed Lists commands, system routines and interfaces, file
and does the minimal amount of recompiling to create formats, and some of the maintenance procedures.
a consistent updated version. You can’t live without this, although you will prob-
The debugger adb is useful for digging through ably only need to read section 1.
the dead bodies of C programs, but is rather hard to Documents for Use with the UNIX Time-sharing Sys-
learn to use effectively. The most effective debug- tem. Volume 2 of the Programmer’s Manual. This
ging tool is still careful thought, coupled with judi- contains more extensive descriptions of major com-
ciously placed print statements. mands, and tutorials and reference manuals. All of
The C compiler provides a limited instrumenta- the papers listed below are in it, as are descriptions of
tion service, so you can find out where programs most of the programs mentioned above.
spend their time and what parts are worth optimizing. D. M. Ritchie and K. L. Thompson, ‘‘The UNIX
Compile the routines with the – p option; after the test Time-sharing System,’’ CACM, July 1974. An over-
run, use prof to print an execution profile. The com-
- 13 -
view of the system, for people interested in operating S. C. Johnson, ‘‘Yacc — Yet Another Compiler-
systems. Worth reading by anyone who programs. Compiler,’’ Bell Laboratories CSTR 32, 1978.
Contains a remarkable number of one-sentence obser- M. E. Lesk, ‘‘Lex — A Lexical Analyzer Genera-
vations on how to do things right. tor,’’ Bell Laboratories CSTR 39, 1975.
The Bell System Technical Journal (BSTJ) Special S. C. Johnson, ‘‘Lint, a C Program Checker,’’ Bell
Issue on UNIX, July/August, 1978, contains many Laboratories CSTR 65, 1977.
papers describing recent developments, and some
retrospective material. S. I. Feldman, ‘‘MAKE — A Program for Maintain-
ing Computer Programs,’’ Bell Laboratories CSTR
The 2nd International Conference on Software 57, 1977.
Engineering (October, 1976) contains several papers
describing the use of the Programmer’s Workbench J. F. Maranzano and S. R. Bourne, ‘‘A Tutorial Intro-
(PWB) version of UNIX. duction to ADB,’’ Bell Laboratories CSTR 62, 1977.
An introduction to a powerful but complex debugging
Document Preparation: tool.
B. W. Kernighan, ‘‘A Tutorial Introduction to the S. I. Feldman and P. J. Weinberger, ‘‘A Portable For-
UNIX Text Editor’’ and ‘‘Advanced Editing on tran 77 Compiler,’’ Bell Laboratories, 1978. A full
UNIX,’’ Bell Laboratories, 1978. Beginners need the Fortran 77 for UNIX systems.
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 pack-
age, 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 probably is. The
most likely alternative is the PWB/UNIX macro pack-
age – mm; see your local guru if you use PWB/UNIX.
B. W. Kernighan and L. L. Cherry, ‘‘A System for
Typesetting Mathematics,’’ Bell Laboratories Com-
puting 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 Manual,’’
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
Laboratories, 1976. An attempt to unravel the intrica-
cies of troff.
Programming:
B. W. Kernighan and D. M. Ritchie, The C Program-
ming Language, Prentice-Hall, 1978. Contains a
tutorial introduction, complete discussions of all
language features, and the reference manual.
B. W. Kernighan and D. M. Ritchie, ‘‘UNIX Program-
ming,’’ 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 refer-
ence manual for the Version 7 shell. Mandatory
reading if you intend to make effective use of the
programming power of this shell.
A Tutorial Introduction to the UNIX Text Editor
Brian W. Kernighan
Bell Laboratories
Murray Hill, New Jersey 07974
ABSTRACT
Almost all text input on the UNIX† operating system is done with the text-editor ed.
This memorandum is a tutorial guide to help beginners get started with text editing.
Although it does not cover everything, it does discuss enough for most users’ day-
to-day needs. This includes printing, appending, changing, deleting, moving and
inserting entire lines of text; reading and writing files; context searching and line
addressing; the substitute command; the global commands; and the use of special char-
acters for advanced editing.
_______________
†UNIX is a Trademark of Bell Laboratories.
A Tutorial Introduction to the UNIX Text Editor
Brian W. Kernighan
Bell Laboratories
Murray Hill, New Jersey 07974
? or
Writing text out as a file – the Write command Reading text from a file – the Edit command ‘‘e’’
‘‘w’’
A common way to get text into the buffer is to
It’s likely that you’ll want to save your text for read it from a file in the file system. This is what
later use. To write out the contents of the buffer onto you do to edit text that you saved with the w com-
a file, use the write command mand in a previous session. The edit command e
w fetches the entire contents of a file into the buffer.
So if you had saved the three lines ‘‘Now is the
followed by the filename you want to write on. This time’’, etc., with a w command in an earlier session,
will copy the buffer’s contents onto the specified file the ed command
(destroying any previous information on the file). To
save the text on a file named junk, for example, type e jjuunnkk
w jjuunnkk would fetch the entire contents of the file junk into
the buffer, and respond
Leave a space between w and the file name. Ed will
respond by printing the number of characters it wrote 6688
out. In this case, ed would respond with which is the number of characters in junk. If any-
6688 thing was already in the buffer, it is deleted first.
(Remember that blanks and the return character at the If you use the e command to read a file into the
end of each line are included in the character count.) buffer, then you need not use a file name after a sub-
Writing a file just makes a copy of the text – the sequent w command; ed remembers the last file name
_____________________
buffer’s contents are not disturbed, so you can go on † Actually, ed will print ? if you try to quit without writing.
adding lines to it. This is an important point. Ed at At that point, write if you want; if not, another q will get you
all times works on a copy of a file, not the file itself. out regardless.
No change in the contents of a file takes place until
-3-
11ss//tthh//tthhee// N
Noow
w iiss tthhee ttiim
mee
This says: ‘‘in line 1, substitute for the characters th Notice that // (two adjacent slashes) means ‘‘no char-
the characters the.’’ To verify that it works (ed will acters’’, not a blank. There is a difference! (See
not print the result automatically) say below for another meaning of //.)
p Exercise 5:
and get Experiment with the substitute command. See
what happens if you substitute for some word on a
N
Noow
w iiss tthhee ttiim
mee
line with several occurrences of that word. For
which is what you wanted. Notice that dot must have example, do this:
been set to the line where the substitution took place,
a
since the p command printed that line. Dot is always
tthhee ootthheerr ssiiddee ooff tthhee ccooiinn
set this way with the s command.
.
The general way to use the substitute command is ss//tthhee//oonn tthhee//pp
starting-line, ending-line ss//change this//to this// You will get
Whatever string of characters is between the first pair
-6-
ttoo ccoom
mee ttoo tthhee aaiidd ooff tthheeiirr ppaarrttyy.. //N
Noow
w//,,//N
Noow
w//+
+22pp
‘‘Next occurrence’’ means that ed starts looking for or by any number of similar combinations. The first
the string at line .+1, searches to the end of the one of these might be better if you don’t know how
buffer, then continues at line 1 and searches to line many lines are involved. (Of course, if there were
dot. (That is, the search ‘‘wraps around’’ from $ to only three lines in the buffer, you’d use
1.) It scans all the lines in the buffer until it either 11,,$$pp
finds the desired line or gets back to dot again. If the
given string of characters can’t be found in any line, but not if there were several hundred.)
ed types the error message
-7-
The basic rule is: a context search expression is is used to replace a number of lines with different
the same as a line number, so it can be used wher- lines, which are typed in at the terminal. For exam-
ever a line number is needed. ple, to change lines .+1 through $ to something else,
type
Exercise 6:
..+
+11,,$$cc
Experiment with context searching. Try a body . . . type the lines of text you want here . . .
of text with several occurrences of the same string of .
characters, and scan through it using the same context
search. The lines you type between the c command and the .
will take the place of the original lines between start
Try using context searches as line numbers for the line and end line. This is most useful in replacing a
substitute, print and delete commands. (They can line or several lines which have errors in them.
also be used with r, w, and a.)
If only one line is specified in the c command,
Try context searching using ?text? instead of then just that line is replaced. (You can type in as
/text/. This scans lines in the buffer in reverse order many replacement lines as you like.) Notice the use
rather than normal. This is sometimes useful if you of . to end the input – this works just like the . in
go too far while looking for some string of characters the append command and must appear by itself on a
– it’s an easy way to back up. new line. If no line number is given, line dot is
(If you get funny results with any of the charac- replaced. The value of dot is set to the last line you
ters typed in.
ˆ . $ [ ∗ \ & ‘‘Insert’’ is similar to append – for instance
FFiirrsstt ppaarraaggrraapphh means ‘‘a line with an x, any character, and a y,’’
... not just ‘‘a line with an x, a period, and a y.’’ A
eenndd ooff fi firrsstt ppaarraaggrraapphh.. complete list of the special characters that can cause
SSeeccoonndd ppaarraaggrraapphh trouble is the following:
... ˆ . $ [ ∗ \
eenndd ooff sseeccoonndd ppaarraaggrraapphh..
Warning: The backslash character \ is special to ed.
you could reverse the two paragraphs like this: For safety’s sake, avoid it where possible. If you
//S
Seeccoonndd//,,//eenndd ooff sseeccoonndd//m
m//F
Fiirrsstt//–– 1 have to use one of the special characters in a substi-
tute command, you can turn off its magic meaning
Notice the – 1: the moved text goes after the line temporarily by preceding it with the backslash. Thus
mentioned. Dot gets set to the last line moved.
ss//\\\\\\.\\∗∗//bbaacckkssllaasshh ddoott ssttaarr//
The global commands ‘‘g’’ and ‘‘v’’ will change \.∗ into ‘‘backslash dot star’’.
The global command g is used to execute one or Here is a hurried synopsis of the other special
more ed commands on all those lines in the buffer characters. First, the circumflex ˆ signifies the begin-
that match some specified string. For example ning of a line. Thus
gg//ppeelliinngg//pp //ˆˆssttrriinngg//
prints all lines that contain peling. More usefully, finds string only if it is at the beginning of a line: it
gg//ppeelliinngg//ss////ppeelllliinngg//ggpp will find
will find only a line that contains just string, and You don’t have to match the whole line, of
course: if the buffer contains
//ˆˆ.$$//
tthhee eenndd ooff tthhee w
woorrlldd
finds a line containing exactly one character.
The character ., as we mentioned above, matches you could type
anything; //w
woorrlldd//ss////&
& iiss aatt hhaanndd//
//xx.yy// to produce
matches any of tthhee eenndd ooff tthhee w
woorrlldd iiss aatt hhaanndd
xx+
+yy Observe this expression carefully, for it illustrates
xx–– y how to take advantage of ed to save typing. The
xy string /world/ found the desired line; the shorthand //
x. y found the same word in the line; and the & saves you
This is useful in conjunction with ∗, which is a from typing it again.
repetition character; a∗ is a shorthand for ‘‘any The & is a special character only within the
number of a’s,’’ so .∗ matches any number of any- replacement text of a substitute command, and has no
things. This is used like this: special meaning elsewhere. You can turn off the spe-
cial meaning of & by preceding it with a \:
ss//.∗∗//ssttuuffff//
ss//aam
mppeerrssaanndd//\\&
&//
which changes an entire line, or
will convert the word ‘‘ampersand’’ into the literal
ss//.∗∗,,////
symbol & in the current line.
which deletes all characters in the line up to and
including the last comma. (Since .∗ finds the longest
possible match, this goes up to the last comma.)
[ is used with ] to form ‘‘character classes’’; for Summary of Commands and Line Numbers
example, The general form of ed commands is the com-
mand name, perhaps preceded by one or two line
//[[00112233445566778899]]//
numbers, and, in the case of e, r, and w, followed by
matches any single digit – any one of the characters a file name. Only one command is allowed per line,
inside the braces will cause a match. This can be but a p command may follow any other command
abbreviated to [0– 9]. (except for e, r, w and q).
Finally, the & is another shorthand character – it a: Append, that is, add lines to the buffer (at line
is used only on the right-hand part of a substitute dot, unless a different line is specified). Appending
command where it means ‘‘whatever was matched on continues until . is typed on a new line. Dot is set to
the left-hand side’’. It is used to save typing. Sup- the last line appended.
pose the current line contained c: Change the specified lines to the new text which
N
Noow
w iiss tthhee ttiim
mee follows. The new lines are terminated by a ., as with
a. If no lines are specified, replace line dot. Dot is
and you wanted to put parentheses around it. You set to last line changed.
could just retype the line, but this is tedious. Or you
could say d: Delete the lines specified. If none are specified,
delete line dot. Dot is set to the first undeleted line,
ss//ˆˆ//((// unless $ is deleted, in which case dot is set to $.
ss//$$//))//
e: Edit new file. Any previous contents of the buffer
using your knowledge of ˆ and $. But the easiest are thrown away, so issue a w beforehand.
way uses the &: f: Print remembered filename. If a name follows f
ss//.∗∗//((&
&))// the remembered name will be set to it.
This says ‘‘match the whole line, and replace it by g: The command
itself surrounded by parentheses.’’ The & can be gg//------//ccoom
mmmaannddss
used several times in a line; consider using
will execute the commands on those lines that contain
ss//.∗∗//&
&?? &
&!!!!// ---, which can be any context search expression.
to produce i: Insert lines before specified line (or dot) until a . is
typed on a new line. Dot is set to last line inserted.
N
Noow
w iiss tthhee ttiim
mee?? N
Noow
w iiss tthhee ttiim
mee!!!!
- 10 -
Brian W. Kernighan
Bell Laboratories
Murray Hill, New Jersey 07974
ABSTRACT
This paper is meant to help secretaries, typists and programmers to make effec-
tive use of the UNIX† facilities for preparing and editing text. It provides explanations
and examples of
• special characters, line addressing and global commands in the editor ed;
• commands for ‘‘cut and paste’’ operations on files and parts of files, including
the mv, cp, cat and rm commands, and the r, w, m and t commands of the edi-
tor;
• editing scripts and editor-based programs like grep and sed.
Although the treatment is aimed at non-programmers, new users with any back-
ground should find helpful hints on how to get their jobs done more easily.
August 4, 1978
_______________
†UNIX is a Trademark of Bell Laboratories.
Advanced Editing on UNIX
Brian W. Kernighan
Bell Laboratories
Murray Hill, New Jersey 07974
With
/x.y/
s/this/that/
finds any line where ‘x’ and ‘y’ occur separated by a
and single character, as in
s/this/that/g x+y
x– y
the first one replaces the first ‘this’ on the line with
x y
‘that’. If there is more than one ‘this’ on the line, the
x. y
second form with the trailing g changes all of them.
Either form of the s command can be followed and so on. (We will use to stand for a space when-
by p or l to ‘print’ or ‘list’ (as described in the previ- ever we need to make it visible.)
ous section) the contents of the line: Since ‘.’ matches a single character, that gives
you a way to deal with funny characters printed by l.
s/this/that/p
Suppose you have a line that, when printed with the l
s/this/that/l
command, appears as
s/this/that/gp
s/this/that/gl .... th\\07is ....
are all legal, and mean slightly different things. and you want to get rid of the \\07 (which represents
Make sure you know what the differences are. the bell character, by the way).
Of course, any s command can be preceded by The most obvious solution is to try
one or two ‘line numbers’ to specify that the substitu-
s/\\07//
tion is to take place on a group of lines. Thus
but this will fail. (Try it.) The brute force solution,
1,$s/mispell/misspell/
which most people would now take, is to re-type the
changes the first occurrence of ‘mispell’ to ‘misspell’ entire line. This is guaranteed, and is actually quite a
on every line of the file. But reasonable tactic if the line in question isn’t too big,
but for a very long line, re-typing is a bore. This is
1,$s/mispell/misspell/g
where the metacharacter ‘.’ comes in handy. Since
changes every occurrence in every line (and this is ‘\\07’ really represents a single character, if we say
more likely to be what you wanted in this particular
s/th.is/this/
case).
You should also notice that if you add a p or l the job is done. The ‘.’ matches the mysterious char-
to the end of any of these substitute commands, only acter between the ‘h’ and the ‘i’, whatever it is.
the last line that got changed will be printed, not all Bear in mind that since ‘.’ matches any single
the lines. We will talk later about how to print all character, the command
the lines that were modified.
s/./,/
The Undo Command ‘u’ converts the first character on a line into a ‘,’, which
Occasionally you will make a substitution in a very often is not what you intended.
line, only to realize too late that it was a ghastly mis- As is true of many characters in ed, the ‘.’ has
take. The ‘undo’ command u lets you ‘undo’ the last several meanings, depending on its context. This line
substitution: the last line that was substituted can be shows all three:
restored to its previous state by typing the command
.s/././
u
The first ‘.’ is a line number, the number of the line
we are editing, which is called ‘line dot’. (We will
The Metacharacter ‘.’ discuss line dot more in Section 3.) The second ‘.’ is
As you have undoubtedly noticed when you a metacharacter that matches any single character on
use ed, certain characters have unexpected meanings that line. The third ‘.’ is the only one that really is
when they occur in the left side of a substitute com- an honest literal period. On the right side of a substi-
mand, or in a search for a particular line. In the next tution, ‘.’ is not special. If you apply this command
several sections, we will talk about these special char- to the line
acters, which are often called ‘metacharacters’. Now is the time.
The first one is the period ‘.’. On the left side the result will be
of a substitute command, or in a search with ‘/.../’, ‘.’
stands for any single character. Thus the search .ow is the time.
which is probably not what you intended.
-3-
and the ‘&’ will stand for ‘the’. Of course this isn’t s/ very /\\
much of a saving if the thing matched is just ‘the’, .ul\\
but if it is something truly long or awful, or if it is very\\
something like ‘.∗’ which matches a lot of text, you /
can save some tedious typing. There is also much converts the line into four shorter lines, preceding the
less chance of making a typing error in the replace- word ‘very’ by the line ‘.ul’, and eliminating the
ment text. For example, to parenthesize a line, spaces around the ‘very’, all at the same time.
regardless of its length,
When a newline is substituted in, dot is left
s/.∗/(&)/ pointing at the last line created.
and so on, and you want the initials to precede the Address Arithmetic
name, as in The next step is to combine the line numbers
A. B. Smith like ‘.’, ‘$’, ‘/.../’ and ‘?...?’ with ‘+’ and ‘– ’. Thus
C. Jones $– 1
It is possible to do this with a series of editing com- is a command to print the next to last line of the
mands, but it is tedious and error-prone. (It is current file (that is, one line before line ‘$’). For
instructive to figure out how it is done, though.) example, to recall how far you got in a previous edit-
The alternative is to ‘tag’ the pieces of the pat- ing session,
tern (in this case, the last name, and the initials), and
$– 5,$p
then rearrange the pieces. On the left side of a sub-
stitution, if part of the pattern is enclosed between \\( prints the last six lines. (Be sure you understand why
and \\), whatever matched that part is remembered, it’s six, not five.) If there aren’t six, of course, you’ll
and available for use on the right side. On the right get an error message.
side, the symbol ‘\\1’ refers to whatever matched the As another example,
first \\(...\\) pair, ‘\\2’ to the second \\(...\\), and so on.
The command
.– 3,.+3p
prints from three lines before where you are now (at
1,$s/ˆ\\([ˆ,]∗\\), ∗\\(.∗\\)/\\2 \\1/
line dot) to three lines after, thus giving you a bit of
although hard to read, does the job. The first \\(...\\) context. By the way, the ‘+’ can be omitted:
matches the last name, which is any string up to the
comma; this is referred to on the right side with ‘\\1’.
.– 3,.3p
The second \\(...\\) is whatever follows the comma and is absolutely identical in meaning.
any spaces, and is referred to as ‘\\2’. Another area in which you can save typing
Of course, with any editing sequence this com- effort in specifying lines is to use ‘– ’ and ‘+’ as line
plicated, it’s foolhardy to simply run it and hope. numbers by themselves.
The global commands g and v discussed in section 4
–
provide a way for you to print exactly those lines
which were affected by the substitute command, and by itself is a command to move back up one line in
thus verify that it did what you wanted in all cases. the file. In fact, you can string several minus signs
together to move back up that many lines:
3. LINE ADDRESSING IN THE EDITOR
–––
The next general area we will discuss is that of
line addressing in ed, that is, how you specify what moves up three lines, as does ‘– 3’. Thus
lines are to be affected by editing commands. We – 3,+3p
have already used constructions like
is also identical to the examples above.
1,$s/x/y/
Since ‘– ’ is shorter than ‘.– 1’, constructions
to specify a change on all lines. And most users are like
long since familiar with using a single newline (or
– ,.s/bad/good/
return) to print the next line, and with
are useful. This changes ‘bad’ to ‘good’ on the previ-
/thing/
ous line and on the current line.
to find a line that contains ‘thing’. Less familiar, ‘+’ and ‘– ’ can be used in combination with
surprisingly enough, is the use of searches using ‘/.../’ and ‘?...?’, and with ‘$’. The
?thing? search
to repeat the search again. You don’t have to re-type The line-changing commands a, c and i by
the search, for the construction default all affect the current line — if you give no
line number with them, a appends text after the
//
current line, c changes the current line, and i inserts
is a shorthand for ‘the previous thing that was text before the current line.
searched for’, whatever it was. This can be repeated a, c, and i behave identically in one respect —
as many times as necessary. You can also go back- when you stop appending, changing or inserting, dot
wards: points at the last line entered. This is exactly what
?? you want for typing and editing on the fly. For
example, you can say
searches for the same thing, but in the reverse direc-
tion. a
... text ...
Not only can you repeat the search, but you
... botch ... (minor error)
can use ‘//’ as the left side of a substitute command,
to mean ‘the most recent pattern’.
.
s/botch/correct/ (fix botched line)
/horrible thing/ a
.... ed prints line with ‘horrible thing’ ... ... more text ...
s//good/p
without specifying any line number for the substitute
To go backwards and change a line, say command or for the second append command. Or
you can say
??s//good/
a
Of course, you can still use the ‘&’ on the right hand
... text ...
side of a substitute to stand for whatever got matched:
... horrible botch ... (major error)
//s//& &/p .
c (replace entire line)
finds the next occurrence of whatever you searched
... fixed up line ...
for last, replaces it by two copies of itself, then prints
the line just to verify that it worked. You should experiment to determine what hap-
pens if you add no lines with a, c or i.
Default Line Numbers and the Value of Dot
The r command will read a file into the text
One of the most effective ways to speed up being edited, either at the end if you give no address,
your editing is always to know what lines will be or after the specified line if you do. In either case,
affected by a command if you don’t specify the lines dot points at the last line read in. Remember that you
it is to act on, and on what line you will be posi- can even say 0r to read a file in at the beginning of
tioned (i.e., the value of dot) when a command the text. (You can also say 0a or 1i to start adding
finishes. If you can edit without specifying unneces- text at the beginning.)
sary line numbers, you can save a lot of typing.
The w command writes out the entire file. If
As the most obvious example, if you issue a you precede the command by one line number, that
search command like line is written, while if you precede it by two line
/thing/ numbers, that range of lines is written. The w com-
mand does not change dot: the current line remains
you are left pointing at the next line that contains the same, regardless of what lines are written. This is
‘thing’. Then no address is required with commands true even if you say something like
like s to make a substitution on that line, or p to print
it, or l to list it, or d to delete it, or a to append text /ˆ\\.AB/,/ˆ\\.AE/w abstract
after it, or c to change it, or i to insert text before it. which involves a context search.
What happens if there was no ‘thing’? Then Since the w command is so easy to use, you
you are left right where you were — dot is should save what you are editing regularly as you go
unchanged. This is also true if you were sitting on along just in case the system crashes, or in case you
the only ‘thing’ when you issued the command. The do something foolish, like clobbering what you’re
same rules hold for searches that use ‘?...?’; the only editing.
difference is the direction in which you search.
The least intuitive behavior, in a sense, is that
The delete command d leaves dot pointing at of the s command. The rule is simple — you are left
the line that followed the last deleted line. When line sitting on the last line that got changed. If there were
‘$’ gets deleted, however, dot points at the new line no changes, then dot is unchanged.
‘$’.
-9-
prints the third line, which is the last one changed. /thing/
But if the three lines had been //
hand, it does no harm to put it in either. Now if you decide at some time that you want
to get back to the original state of ‘good’, you can
5. CUT AND PASTE WITH UNIX COMMANDS say
One editing area in which non-programmers mv savegood good
seem not very confident is in what might be called
‘cut and paste’ operations — changing the name of a (if you’re not interested in ‘savegood’ any more), or
file, making a copy of a file somewhere else, moving cp savegood good
a few lines from one place to another in a file, insert-
ing one file in the middle of another, splitting a file if you still want to retain a safe copy.
into pieces, and splicing two or more files together. In summary, mv just renames a file; cp makes
Yet most of these operations are actually quite a duplicate copy. Both of them clobber the ‘target’
easy, if you keep your wits about you and go cau- file if it already exists, so you had better be sure
tiously. The next several sections talk about cut and that’s what you want to do before you do it.
paste. We will begin with the UNIX commands for
moving entire files around, then discuss ed commands Removing a File
for operating on pieces of files. If you decide you are really done with a file
forever, you can remove it with the rm command:
Changing the Name of a File
rm savegood
You have a file named ‘memo’ and you want it
to be called ‘paper’ instead. How is it done? throws away (irrevocably) the file called ‘savegood’.
The UNIX program that renames files is called Putting Two or More Files Together
mv (for ‘move’); it ‘moves’ the file from one name to
another, like this: The next step is the familiar one of collecting
two or more files into one big one. This will be
mv memo paper needed, for example, when the author of a paper
That’s all there is to it: mv from the old name to the decides that several sections need to be combined into
new name. one. There are several ways to do it, of which the
cleanest, once you get used to it, is a program called
mv oldname newname cat. (Not all programs have two-letter names.) cat is
Warning: if there is already a file around with the short for ‘concatenate’, which is exactly what we
new name, its present contents will be silently clob- want to do.
bered by the information from the other file. The one Suppose the job is to combine the files ‘file1’
exception is that you can’t move a file to itself — and ‘file2’ into a single file called ‘bigfile’. If you
say
mv x x
cat file
is illegal.
the contents of ‘file’ will get printed on your terminal.
Making a Copy of a File If you say
Sometimes what you want is a copy of a file cat file1 file2
— an entirely fresh version. This might be because
you want to work on a file, and yet save a copy in the contents of ‘file1’ and then the contents of ‘file2’
case something gets fouled up, or just because you’re will both be printed on your terminal, in that order.
paranoid. So cat combines the files, all right, but it’s not much
help to print them on the terminal — we want them
In any case, the way to do it is with the cp in ‘bigfile’.
command. (cp stands for ‘copy’; the system is big on
short command names, which are appreciated by Fortunately, there is a way. You can tell the
heavy users, but sometimes a strain for novices.) system that instead of printing on your terminal, you
Suppose you have a file called ‘good’ and you want want the same information put in a file. The way to
to save a copy before you make some dramatic edit- do it is to add to the command line the character >
ing changes. Choose a name — ‘savegood’ might be and the name of the file where you want the output to
acceptable — then type go. Then you can say
This copies ‘good’ onto ‘savegood’, and you now and the job is done. (As with cp and mv, you’re put-
have two identical copies of the file ‘good’. (If ting something into ‘bigfile’, and anything that was
‘savegood’ previously contained something, it gets already there is destroyed.)
overwritten.) This ability to ‘capture’ the output of a pro-
- 12 -
which is the way a table is set up for the tbl program. As we said, that’s the brute force way. The
To isolate the table in a separate file called ‘table’, easier way (often) is to use the move command m
first find the start of the table (the ‘.TS’ line), then that ed provides — it lets you do the whole set of
write out the interesting part: operations at one crack, without any temporary file.
The m command is like many other ed com-
/ˆ\\.TS/
mands in that it takes up to two line numbers in front
.TS [ed prints the line it found] that tell what lines are to be affected. It is also fol-
.,/ˆ\\.TE/w table lowed by a line number that tells where the lines are
and the job is done. If you are confident, you can do to go. Thus
it all at once with
line1, line2 m line3
/ˆ\\.TS/;/ˆ\\.TE/w table
says to move all the lines between ‘line1’ and ‘line2’
The point is that the w command can write out after ‘line3’. Naturally, any of ‘line1’ etc., can be
a group of lines, instead of the whole file. In fact, patterns between slashes, $ signs, or other ways to
you can write out a single line if you like; just give specify lines.
one line number instead of two. For example, if you Suppose again that you’re sitting at the first
have just typed a horribly complicated line and you line of the paragraph. Then you can say
know that it (or something like it) is going to be
.,/ˆ\\.PP/– m$
needed later, then save it — don’t re-type it. In the
editor, say That’s all.
a As another example of a frequent operation,
...lots of stuff... you can reverse the order of two adjacent lines by
...horrible line... moving the first one to after the second. Suppose that
. you are positioned at the first. Then
.w temp m+
a
...more stuff... does it. It says to move line dot to after one line
. after line dot. If you are positioned on the second
.r temp line,
a m– –
...more stuff...
. does the interchange.
This last example is worth studying, to be sure you As you can see, the m command is more suc-
appreciate what’s going on. cinct and direct than writing, deleting and re-reading.
When is brute force better anyway? This is a matter
of personal taste — do what you have most
- 14 -
If a fairly complicated set of editing operations I am grateful to Ted Dolotta for his careful
is to be done on a whole set of files, the easiest thing reading and valuable suggestions.
to do is to make up a ‘script’, i.e., a file that contains
the operations you want to perform, then apply this References
script to each file in turn. [1] Brian W. Kernighan, A Tutorial Introduction to
For example, suppose you want to change the UNIX Text Editor, Bell Laboratories inter-
every ‘Unix’ to ‘UNIX’ and every ‘Gcos’ to ‘GCOS’ nal memorandum.
in a large number of files. Then put into the file [2] Brian W. Kernighan, UNIX For Beginners,
‘script’ the lines Bell Laboratories internal memorandum.
g/Unix/s//UNIX/g [3] Ken L. Thompson and Dennis M. Ritchie, The
g/Gcos/s//GCOS/g UNIX Programmer’s Manual. Bell Labora-
w tories.
q
Now you can say
ed file1 <script
ed file2 <script
...
This causes ed to take its commands from the
prepared script. Notice that the whole job has to be
planned in advance.
And of course by using the UNIX command
interpreter, you can cycle through a set of files
automatically, with varying degrees of ease.
An Introduction to the UNIX Shell
S. R. Bourne
Bell Laboratories
Murray Hill, New Jersey 07974
ABSTRACT
The shell is a command programming language that provides an interface to the UNIX†
operating system. Its features include control-flow primitives, parameter passing, vari-
ables and string substitution. Constructs such as while, if then else, case and for are
available. Two-way communication is possible between the shell and commands.
String-valued parameters, typically file names or flags, may be passed to a command.
A return code is set by commands that may be used to determine control-flow, and the
standard output from a command may be used as shell input.
The shell can modify the environment in which commands run. Input and output can
be redirected to files, and processes that communicate through ‘pipes’ can be invoked.
Commands are found by searching directories in the file system in a sequence that can
be defined by the user. Commands can be read either from the terminal or from a file,
which allows command procedures to be stored for later use.
_______________
†UNIX is a Trademark of Bell Laboratories.
An Introduction to the UNIX Shell
S. R. Bourne
Bell Laboratories
Murray Hill, New Jersey 07974
1.0 Introduction
The shell is both a command language and a programming language that provides an interface to the
UNIX operating system. This memorandum describes, with examples, the UNIX shell. The first section
covers most of the everyday requirements of terminal users. Some familiarity with UNIX is an advan-
tage when reading this section; see, for example, "UNIX for beginners".1 Section 2 describes those
features of the shell primarily intended for use within shell procedures. These include the control-flow
primitives and string-valued variables provided by the shell. A knowledge of a programming language
would be a help when reading this section. The last section describes the more advanced features of the
shell. References of the form "see pipe (2)" are to a section of the UNIX manual.2
The standard input of a command may be taken from a file instead of the terminal by writing, for exam-
ple,
wc <file
The command wc reads its standard input (in this case redirected from file) and prints the number of
characters, words and lines found. If only the number of lines is required then
wc −l <file
could be used.
[a−z]*
matches all names in the current directory beginning with one of the letters a through z.
/usr/fred/test/?
matches all names in the directory /usr/fred/test that consist of a single character. If no file name is
found that matches the pattern then the pattern is passed, unchanged, as an argument.
This mechanism is useful both to save typing and to select names according to some pattern. It may
also be used to find files. For example,
echo /usr/fred/*/core
finds and prints the names of all core files in sub-directories of /usr/fred . (echo is a standard UNIX
command that prints its arguments, separated by blanks.) This last feature can be expensive, requiring a
scan of all sub-directories of /usr/fred .
There is one exception to the general rules given for patterns. The character ‘.’ at the start of a file
name must be explicitly matched.
echo *
will therefore echo all file names in the current directory not beginning with ‘.’ .
echo .*
will echo all those file names that begin with ‘.’ . This avoids inadvertent matching of the names ‘.’ and
‘..’ which mean ‘the current directory’ and ‘the parent directory’ respectively. (Notice that ls
suppresses information for the files ‘.’ and ‘..’ .)
1.6 Quoting
Characters that have a special meaning to the shell, such as < > * ? | & , are called metacharacters. A
complete list of metacharacters is given in appendix B. Any character preceded by a \ is quoted and
loses its special meaning, if any. The \ is elided so that
echo \?
will echo a single ? , and
echo \\
will echo a single \ . To allow long strings to be continued over more than one line the sequence \new-
line is ignored.
\ is convenient for quoting single characters. When more than one character needs quoting the above
mechanism is clumsy and error prone. A string of characters may be quoted by enclosing the string
between single quotes. For example,
echo xx´****´xx
will echo
xx****xx
The quoted string may not contain a single quote but may contain newlines, which are preserved. This
quoting mechanism is the most simple and is recommended for casual use.
A third quoting mechanism using double quotes is also available that prevents interpretation of some but
not all metacharacters. Discussion of the details is deferred to section 3.4 .
-4-
1.7 Prompting
When the shell is used from a terminal it will issue a prompt before reading a command. By default
this prompt is ‘$ ’ . It may be changed by saying, for example,
PS1=yesdear
that sets the prompt to be the string yesdear . If a newline is typed and further input is needed then the
shell will issue the prompt ‘> ’ . Sometimes this can be caused by mistyping a quote mark. If it is
unexpected then an interrupt (DEL) will return the shell to read another command. This prompt may be
changed by saying, for example,
PS2=more
1.9 Summary
• ls
Print the names of files in the current directory.
• ls >file
Put the output from ls into file.
• ls | wc −l
Print the number of files in the current directory.
• ls | grep old
Print those file names containing the string old.
• ls | grep old | wc −l
Print the number of files whose name contains the string old.
• cc pgm.c &
Run cc in the background.
-5-
UNIX files have three independent attributes, read, write and execute. The UNIX command chmod (1)
may be used to make a file executable. For example,
chmod +x wg
will ensure that the file wg has execute status. Following this, the command
wg fred
is equivalent to
sh wg fred
This allows shell procedures and programs to be used interchangeably. In either case a new process is
created to run the command.
As well as providing names for the positional parameters, the number of positional parameters in the call
is available as $# . The name of the file being executed is available as $0 .
A special shell parameter $* is used to substitute for all positional parameters except $0 . A typical use
of this is to provide some default arguments, as in,
nroff −T450 −ms $*
which simply prepends some arguments to those already given.
case $# in
*) . . . ;;
*) . . . ;;
esac
Another example of the use of the case construction is to distinguish between different forms of an argu-
ment. The following example is a fragment of a cc command.
for i
do case $i in
−[ocs]) . . . ;;
−*) echo ´unknown flag $i´ ;;
*.c) /lib/c0 $i . . . ;;
*) echo ´unexpected argument $i´ ;;
esac
done
To allow the same commands to be associated with more than one pattern the case command provides
for alternative patterns separated by a | . For example,
case $i in
−x | −y) ...
esac
is equivalent to
case $i in
−[xy]) ...
esac
ed $3 <<%
g/$1/s//$2/g
w
%
The call
edg string1 string2 file
is then equivalent to the command
ed file <<%
g/string1/s//string2/g
w
%
and changes all occurrences of string1 in file to string2 . Substitution can be prevented using \ to quote
the special character $ as in
ed $3 <<+
1,\$s/$1/$2/g
w
+
(This version of edg is equivalent to the first except that ed will print a ? if there are no occurrences of
the string $1 .) Substitution within a here document may be prevented entirely by quoting the terminat-
ing string, for example,
grep $i <<\#
...
#
The document is presented without modification to grep. If parameter substitution is not required in a
here document this latter form is more efficient.
echo $user
and is used when the parameter name is followed by a letter or digit. For example,
tmp=/tmp/ps
ps a >${tmp}a
will direct the output of ps to the file /tmp/psa, whereas,
ps a >$tmpa
would cause the value of the variable tmpa to be substituted.
Except for $? the following are set initially by the shell. $? is set after executing each command.
$? The exit status (return code) of the last command executed as a decimal string. Most
commands return a zero exit status if they complete successfully, otherwise a non-zero
exit status is returned. Testing the value of return codes is dealt with later under if and
while commands.
$# The number of positional parameters (in decimal). Used, for example, in the append
command to check the number of parameters.
$$ The process number of this shell (in decimal). Since process numbers are unique among
all existing processes, this string is frequently used to generate unique temporary file
names. For example,
ps a >/tmp/ps$$
...
rm /tmp/ps$$
$! The process number of the last process run in the background (in decimal).
$− The current shell flags, such as −x and −v .
Some variables have a special meaning to the shell and should be avoided for general use.
$MAIL When used interactively the shell looks at the file specified by this variable before it
issues a prompt. If the specified file has been modified since it was last looked at the
shell prints the message you have mail before prompting for the next command. This
variable is typically set in the file .profile, in the user’s login directory. For example,
MAIL=/usr/mail/fred
$HOME The default argument for the cd command. The current directory is used to resolve file
name references that do not begin with a / , and is changed using the cd command. For
example,
cd /usr/fred/bin
makes the current directory /usr/fred/bin .
cat wn
will print on the terminal the file wn in this directory. The command cd with no argu-
ment is equivalent to
cd $HOME
This variable is also typically set in the the user’s login profile.
$PATH A list of directories that contain commands (the search path ). Each time a command is
- 10 -
executed by the shell a list of directories is searched for an executable file. If $PATH is
not set then the current directory, /bin, and /usr/bin are searched by default. Otherwise
$PATH consists of directory names separated by : . For example,
PATH=:/usr/fred/bin:/bin:/usr/bin
specifies that the current directory (the null string before the first : ), /usr/fred/bin, /bin
and /usr/bin are to be searched in that order. In this way individual users can have their
own ‘private’ commands that are accessible independently of the current directory. If
the command name contains a / then this directory search is not used; a single attempt is
made to execute the command.
$PS1 The primary shell prompt string, by default, ‘$ ’.
$PS2 The shell prompt when further input is needed, by default, ‘> ’.
$IFS The set of characters used by blank interpretation (see section 3.4).
The value tested by the while command is the exit status of the last simple command following while.
Each time round the loop command-list1 is executed; if a zero exit status is returned then command-list2
is executed; otherwise, the loop terminates. For example,
while test $1
do . . .
shift
done
is equivalent to
for i
do . . .
done
shift is a shell command that renames the positional parameters $2, $3, . . . as $1, $2, . . . and loses $1 .
Another kind of use for the while/until loop is to wait until some external event occurs and then run
some commands. In an until loop the termination condition is reversed. For example,
- 11 -
An example of the use of if, case and for constructions is given in section 2.10 .
A multiple test if command of the form
if . . .
then ...
else if . . .
then ...
else if . . .
...
fi
fi
fi
may be written using an extension of the if notation as,
if . . .
then ...
elif ...
then ...
elif ...
...
fi
The following example is the touch command which changes the ‘last modified’ time for a list of files.
The command may be used in conjunction with make (1) to force recompilation of a list of files.
- 12 -
flag=
for i
do case $i in
−c) flag=N ;;
*) if test −f $i
then ln $i junk$$; rm junk$$
elif test $flag
then echo file \´$i\´ does not exist
else >$i
fi
esac
done
The −c flag is used in this command to force subsequent files to be created if they do not already exist.
Otherwise, if the file does not exist, an error message is printed. The shell variable flag is set to some
non-null string if the −c argument is encountered. The commands
ln . . .; rm . . .
make a link to the file and then remove it thus causing the last modified date to be updated.
The sequence
if command1
then command2
fi
may be written
command1 && command2
Conversely,
command1 | | command2
executes command2 only if command1 fails. In each case the value returned is that of the last simple
command executed.
In the first command-list is simply executed. The second form executes command-list as a separate pro-
cess. For example,
(cd x; rm junk )
executes rm junk in the directory x without changing the current directory of the invoking shell.
The commands
cd x; rm junk
have the same effect but leave the invoking shell in the directory x.
- 13 -
cd /usr/man
for i
do case $i in
[1−9]*) s=$i ;;
−t) N=t ;;
−n) N=n ;;
−*) echo unknown flag \´$i\´ ;;
*) if test −f man$s/$i.$s
then ${N}roff man0/${N}aa man$s/$i.$s
else : ´look through all manual sections´
found=no
for j in 1 2 3 4 5 6 7 8 9
do if test −f man$j/$i.$j
then man $j $i
found=yes
fi
done
case $found in
no) echo ´$i: manual page not found´
esac
fi
esac
done
Figure 1. A version of the man command
- 15 -
echo ${d=.}
which substitutes the same string as
echo ${d−.}
and if d were not previously set then it will be set to the string ‘.’ . (The notation ${. . .=. . .} is not
available for positional parameters.)
If there is no sensible default then the notation
echo ${d?message}
will echo the value of the variable d if it has one, otherwise message is printed by the shell and execu-
tion of the shell procedure is abandoned. If message is absent then a standard message is printed. A
shell procedure that requires some parameters to be set might start as follows.
: ${user?} ${acct?} ${bin?}
...
Colon (:) is a command that is built in to the shell and does nothing once its arguments have been
evaluated. If any of the variables user, acct or bin are not set then the shell will abandon execution of
the procedure.
The entire string between grave accents (`. . .`) is taken as the command to be executed and is replaced
with the output from the command. The command is written using the usual quoting conventions except
that a ` must be escaped using a \ . For example,
ls `echo "$1"`
is equivalent to
ls $1
Command substitution occurs in all contexts where parameter substitution occurs (including here docu-
ments) and the treatment of the resulting text is the same in both cases. This mechanism allows string
processing commands to be used within shell procedures. An example of such a command is basename
which removes a specified suffix from a string. For example,
basename main.c .c
will print the string main . Its use is illustrated by the following fragment from a cc command.
case $A in
...
*.c) B=`basename $A .c`
...
esac
that sets B to the part of $A with the suffix .c stripped.
Here are some composite examples.
- 17 -
The following table gives, for each quoting mechanism, the shell metacharacters that are evaluated.
metacharacter
\ $ * ` " ´
´ n n n n n t
` y n n t n n
" y y n y t n
t terminator
y interpreted
n not interpreted
In cases where more than one evaluation of a string is required the built-in command eval may be used.
For example, if the variable X has the value $y, and if y has the value pqr then
eval echo $X
will echo the string pqr .
In general the eval command evaluates its arguments (as do all commands) and treats the result as input
to the shell. The input is read and the resulting command(s) executed. For example,
wg=´eval who | grep´
$wg fred
is equivalent to
who | grep fred
In this example, eval is required since there is no interpretation of metacharacters, such as | , following
substitution.
Those signals marked with an asterisk produce a core dump if not caught. However, the shell itself
ignores quit which is the only external signal that can cause a dump. The signals in this list of potential
interest to shell programs are 1, 2, 3, 14 and 15.
flag=
trap ´rm −f junk$$; exit´ 1 2 3 15
for i
do case $i in
−c) flag=N ;;
*) if test −f $i
then ln $i junk$$; rm junk$$
elif test $flag
then echo file \´$i\´ does not exist
else >$i
fi
esac
done
The trap command appears before the creation of the temporary file; otherwise it would be possible for
the process to die without removing the file.
Since there is no signal 0 in UNIX it is used by the shell to indicate the commands to be executed on
exit from the shell procedure.
A procedure may, itself, elect to ignore signals by specifying the null string as the argument to trap.
The following fragment is taken from the nohup command.
trap ´´ 1 2 3 15
which causes hangup, interrupt, quit and kill to be ignored both by the procedure and by invoked com-
mands.
Traps may be reset by saying
trap 2 3
which resets the traps for signals 2 and 3 to their default values. A list of the current values of traps
may be obtained by writing
trap
The procedure scan (Figure 5) is an example of the use of trap where there is no exit in the trap com-
mand. scan takes each directory in the current directory, prompts with its name, and then executes com-
mands typed at the terminal until an end of file or an interrupt is received. Interrupts are ignored while
executing the requested commands but cause termination when scan is waiting for input.
d=`pwd`
for i in *
do if test −d $d/$i
then cd $d/$i
while echo "$i:"
trap exit 2
read x
do trap : 2; eval $x; done
fi
done
read x is a built-in command that reads one line from the standard input and places the result in the
- 21 -
variable x . It returns a non-zero exit status if either an end-of-file is read or an interrupt is received.
ed file &
would allow both the editor and the shell to read from the same input at the same time.
The other modification to the environment of a background command is to turn off the QUIT and
INTERRUPT signals so that they are ignored by the command. This allows these signals to be used at
the terminal without causing background commands to terminate. For this reason the UNIX convention
for a signal is that if it is set to 1 (ignored) then it is never changed even for a short time. Note that the
shell command trap has no effect for an ignored signal.
Acknowledgements
The design of the shell is based in part on the original UNIX shell3 and the PWB/UNIX shell,4 some
features having been taken from both. Similarities also exist with the command interpreters of the Cam-
bridge Multiple Access System5 and of CTSS.6
I would like to thank Dennis Ritchie and John Mashey for many discussions during the design of the
shell. I am also grateful to the members of the Computing Science Research Center and to Joe Maran-
zano for their comments on drafts of this document.
References
1. B. W. Kernighan, UNIX for Beginners, 1978.
2. K. Thompson and D. M. Ritchie, UNIX Programmer’s Manual, Bell Laboratories (1978). Seventh
Edition.
3. K. Thompson, ‘‘The UNIX Command Language,’’ pp. 375-384 in Structured Programming—
Infotech State of the Art Report, Infotech International Ltd., Nicholson House, Maidenhead,
Berkshire, England (March 1975).
4. J. R. Mashey, PWB/UNIX Shell Tutorial, September 30, 1977.
5. D. F. Hartley (Ed.), The Cambridge Multiple Access System – Users Reference Manual, Univer-
sity Mathematical Laboratory, Cambridge, England (1968).
6. P. A. Crisman (Ed.), The Compatible Time-Sharing System, M.I.T. Press, Cambridge, Mass.
(1965).
- 23 -
Appendix A - Grammar
item: word
input-output
name = value
simple-command: item
simple-command item
command: simple-command
( command-list )
{ command-list }
for name do command-list done
for name in word . . . do command-list done
while command-list do command-list done
until command-list do command-list done
case word in case-part . . . esac
if command-list then command-list else-part fi
pipeline: command
pipeline | command
andor: pipeline
andor && pipeline
andor | | pipeline
command-list: andor
command-list ;
command-list &
command-list ; andor
command-list & andor
file: word
& digit
&−
pattern: word
pattern | word
empty:
digit: 0123456789
- 24 -
b) patterns
* match any character(s) including none
? match any single character
[...] match any of the enclosed characters
c) substitution
${...} substitute shell variable
`...` substitute command output
d) quoting
\ quote the next character
´...´ quote the enclosed characters except for ´
"..." quote the enclosed characters except for $ ` \ "
e) reserved words
if then else elif fi
case in esac
for while until do done
{ }
LEARN — Computer-Aided Instruction on UNIX
(Second Edition)
Brian W. Kernighan
Michael E. Lesk
Bell Laboratories
Murray Hill, New Jersey 07974
ABSTRACT
This paper describes the second version of the learn program for interpreting
CAI scripts on the UNIX† operating system, and a set of scripts that provide a compu-
terized introduction to the system.
Six current scripts cover basic commands and file handling, the editor, additional
file handling commands, the eqn program for mathematical typing, the ‘‘– ms’’ package
of formatting macros, and an introduction to the C programming language. These
scripts now include a total of about 530 lessons.
Many users from a wide variety of backgrounds have used learn to acquire basic
UNIX skills. Most usage involves the first two scripts, an introduction to UNIX files
and commands, and the UNIX editor.
The second version of learn is about four times faster than the previous one in
CPU utilization, and much faster in perceived time because of better overlap of com-
puting and printing. It also requires less file space than the first version. Many of the
lessons have been revised; new material has been added to reflect changes and
enhancements in UNIX itself. Script-writing is also easier because of revisions to the
script language.
_______________
†UNIX is a Trademark of Bell Laboratories.
LEARN — Computer-Aided Instruction on UNIX
(Second Edition)
Brian W. Kernighan
Michael E. Lesk
Bell Laboratories
Murray Hill, New Jersey 07974
_______________________________________________
Figure 1: Sample dialog from basic files script
(Student responses in italics; ‘$’ is the prompt)
A file can be printed on your terminal
by using the "cat" command. Just say
"cat file" where "file" is the file name.
For example, there is a file named
"food" in this directory. List it
by saying "cat food"; then type "ready".
$ cat food
this is the file
named food.
$ ready
Good. Lesson 3.3a (1)
Of course, you can print any file with "cat".
In particular, it is common to first use
"ls" to find the name of a file and then "cat"
to print it. Note the difference between
"ls", which tells you the name of the file,
and "cat", which tells you the contents.
One file in the current directory is named for
a President. Print the file, then type "ready".
$ cat President
cat: can’t open President
$ ready
Sorry, that’s not right. Do you want to try again? yes
Try the problem again.
$ ls
.ocopy
X1
roosevelt
$ cat roosevelt
this file is named roosevelt
and contains three lines of
text.
$ ready
Good. Lesson 3.3b (0)
The "cat" command can also print several files
at once. In fact, it is named "cat" as an abbreviation
_______________________________________________
for "concatenate"....
To avoid boring the faster students, however, an effort is made in the files and editor scripts to
provide three tracks of different difficulty. The fastest sequence of lessons is aimed at roughly the bulk
and speed of a typical tutorial manual and should be adequate for review and for well-prepared students.
The next track is intended for most users and is roughly twice as long. Typically, for example, the fast
track might present an idea and ask for a variation on the example shown; the normal track will first ask
the student to repeat the example that was shown before attempting a variation. The third and slowest
-3-
track, which is often three or four times the length of the fast track, is intended to be adequate for any-
one. (The lessons of Figure 1 are from the third track.) The multiple tracks also mean that a student
repeating a course is unlikely to hit the same series of lessons; this makes it profitable for a shaky user
to back up and try again, and many students have done so.
The tracks are not completely distinct, however. Depending on the number of correct answers the
student has given for the last few lessons, the program may switch tracks. The driver is actually capable
of following an arbitrary directed graph of lesson sequences, as discussed in section 5. Some more
structured arrangement, however, is used in all current scripts to aid the script writer in organizing the
material into lessons. It is sufficiently difficult to write lessons that the three-track theory is not fol-
lowed very closely except in the files and editor scripts. Accordingly, in some cases, the fast track is
produced merely by skipping lessons from the slower track. In others, there is essentially only one
track.
The main reason for using the learn program rather than simply writing the same material as a
workbook is not the selection of tracks, but actual hands-on experience. Learning by doing is much
more effective than pencil and paper exercises.
Learn also provides a mechanical check on performance. The first version in fact would not let
the student proceed unless it received correct answers to the questions it set and it would not tell a stu-
dent the right answer. This somewhat Draconian approach has been moderated in version 2. Lessons
are sometimes badly worded or even just plain wrong; in such cases, the student has no recourse. But if
a student is simply unable to complete one lesson, that should not prevent access to the rest. Accord-
ingly, the current version of learn allows the student to skip a lesson that he cannot pass; a ‘‘no’’
answer to the ‘‘Do you want to try again?’’ question in Figure 1 will pass to the next lesson. It is still
true that learn will not tell the student the right answer.
Of course, there are valid objections to the assumptions above. In particular, some students may
object to not understanding what they are doing; and the procedure of smashing everything into small
pieces may provoke the retort ‘‘you can’t cross a ditch in two jumps.’’ Since writing CAI scripts is
considerably more tedious than ordinary manuals, however, it is safe to assume that there will always be
alternatives to the scripts as a way of learning. In fact, for a reference manual of 3 or 4 pages it would
not be surprising to have a tutorial manual of 20 pages and a (multi-track) script of 100 pages. Thus the
reference manual will exist long before the scripts.
2. Scripts.
As mentioned above, the present scripts try at most to follow a three-track theory. Thus little of
the potential complexity of the possible directed graph is employed, since care must be taken in lesson
construction to see that every necessary fact is presented in every possible path through the units. In
addition, it is desirable that every unit have alternate successors to deal with student errors.
In most existing courses, the first few lessons are devoted to checking prerequisites. For example,
before the student is allowed to proceed through the editor script the script verifies that the student
understands files and is able to type. It is felt that the sooner lack of student preparation is detected, the
easier it will be on the student. Anyone proceeding through the scripts should be getting mostly correct
answers; otherwise, the system will be unsatisfactory both because the wrong habits are being learned
and because the scripts make little effort to deal with wrong answers. Unprepared students should not
be encouraged to continue with scripts.
There are some preliminary items which the student must know before any scripts can be tried. In
particular, the student must know how to connect to a UNIX† system, set the terminal properly, log in,
and execute simple commands (e.g., learn itself). In addition, the character erase and line kill conven-
tions (# and @) should be known. It is hard to see how this much could be taught by computer-aided
instruction, since a student who does not know these basic skills will not be able to run the learning pro-
gram. A brief description on paper is provided (see Appendix A), although assistance will be needed for
the first few minutes. This assistance, however, need not be highly skilled.
__________________
†UNIX is a Trademark of Bell Laboratories.
-4-
The first script in the current set deals with files. It assumes the basic knowledge above and
teaches the student about the ls , cat , mv , rm , cp and diff commands. It also deals with the abbrevia-
tion characters *, ?, and [ ] in file names. It does not cover pipes or I/O redirection, nor does it present
the many options on the ls command.
This script contains 31 lessons in the fast track; two are intended as prerequisite checks, seven are
review exercises. There are a total of 75 lessons in all three tracks, and the instructional passages typed
at the student to begin each lesson total 4,476 words. The average lesson thus begins with a 60-word
message. In general, the fast track lessons have somewhat longer introductions, and the slow tracks
somewhat shorter ones. The longest message is 144 words and the shortest 14.
The second script trains students in the use of the UNIX context editor ed , a sophisticated editor
using regular expressions for searching.2 All editor features except encryption, mark names and ‘;’ in
addressing are covered. The fast track contains 2 prerequisite checks, 93 lessons, and a review lesson.
It is supplemented by 146 additional lessons in other tracks.
A comparison of sizes may be of interest. The ed description in the reference manual is 2,572
words long. The ed tutorial3 is 6,138 words long. The fast track through the ed script is 7,407 words of
explanatory messages, and the total ed script, 242 lessons, has 15,615 words. The average ed lesson is
thus also about 60 words; the largest is 171 words and the smallest 10. The original ed script represents
about three man-weeks of effort.
The advanced file handling script deals with ls options, I/O diversion, pipes, and supporting pro-
grams like pr , wc , tail , spell and grep . (The basic file handling script is a prerequisite.) It is not as
refined as the first two scripts; this is reflected at least partly in the fact that it provides much less of a
full three-track sequence than they do. On the other hand, since it is perceived as ‘‘advanced,’’ it is
hoped that the student will have somewhat more sophistication and be better able to cope with it at a
reasonably high level of performance.
A fourth script covers the eqn language for typing mathematics. This script must be run on a ter-
minal capable of printing mathematics, for instance the DASI 300 and similar Diablo-based terminals, or
the nearly extinct Model 37 teletype. Again, this script is relatively short of tracks: of 76 lessons, only
17 are in the second track and 2 in the third track. Most of these provide additional practice for stu-
dents who are having trouble in the first track.
The – ms script for formatting macros is a short one-track only script. The macro package it
describes is no longer the standard, so this script will undoubtedly be superseded in the future. Further-
more, the linear style of a single learn script is somewhat inappropriate for the macros, since the macro
package is composed of many independent features, and few users need all of them. It would be better
to have a selection of short lesson sequences dealing with the features independently.
The script on C is in a state of transition. It was originally designed to follow a tutorial on C, but
that document has since become obsolete. The current script has been partially converted to follow the
order of presentation in The C Programming Language,4 but this job is not complete. The C script was
never intended to teach C; rather it is supposed to be a series of exercises for which the computer pro-
vides checking and (upon success) a suggested solution.
This combination of scripts covers much of the material which any UNIX user will need to know to
make effective use of the system. With enlargement of the advanced files course to include more on the
command interpreter, there will be a relatively complete introduction to UNIX available via learn.
Although we make no pretense that learn will replace other instructional materials, it should provide a
useful supplement to existing tutorials and reference manuals.
2000 lessons over two weeks that include Christmas and New Year. Users have ranged in age from six
up.
It is difficult to characterize typical sessions with the scripts; many instances exist of someone
doing one or two lessons and then logging out, as do instances of someone pausing in a script for
twenty minutes or more. In the earlier version of learn , the average session in the files course took 32
minutes and covered 23 lessons. The distribution is quite broad and skewed, however; the longest ses-
sion was 130 minutes and there were five sessions shorter than five minutes. The average lesson took
about 80 seconds. These numbers are roughly typical for non-programmers; a UNIX expert can do the
scripts at approximately 30 seconds per lesson, most of which is the system printing.
At present working through a section of the middle of the files script took about 1.4 seconds of
processor time per lesson, and a system expert typing quickly took 15 seconds of real time per lesson.
A novice would probably take at least a minute. Thus a UNIX system could support ten students work-
ing simultaneously with some spare capacity.
___________________________________________________
Figure 2: Directory structure for learn
lib
play
student1
files for student1...
student2
files for student2...
files
L0.1a lessons for files course
L0.1b
...
editor
...
(other courses)
___________________________________________________
log
When learn is executed, it makes a private directory for the user to work in, within the learn por-
tion of the file system. A fresh copy of all the files used in each lesson (mostly data for the student to
operate upon) is made each time a student starts a lesson, so the script writer may assume that every-
thing is reinitialized each time a lesson is entered. The student directory is deleted after each session;
any permanent records must be kept elsewhere.
-6-
The script writer must provide certain basic items in each lesson:
(1) the text of the lesson;
(2) the set-up commands to be executed before the user gets control;
(3) the data, if any, which the user is supposed to edit, transform, or otherwise process;
(4) the evaluating commands to be executed after the user has finished the lesson, to decide whether
the answer is right; and
(5) a list of possible successor lessons.
Learn tries to minimize the work of bookkeeping and installation, so that most of the effort involved in
script production is in planning lessons, writing tutorial paragraphs, and coding tests of student perfor-
mance.
The basic sequence of events is as follows. First, learn creates the working directory. Then, for
each lesson, learn reads the script for the lesson and processes it a line at a time. The lines in the script
are: (1) commands to the script interpreter to print something, to create a files, to test something, etc.;
(2) text to be printed or put in a file; (3) other lines, which are sent to the shell to be executed. One line
in each lesson turns control over to the user; the user can run any UNIX commands. The user mode ter-
minates when the user types yes , no , ready , or answer . At this point, the user’s work is tested; if the
lesson is passed, a new lesson is selected, and if not the old one is repeated.
Let us illustrate this with the script for the second lesson of Figure 1; this is shown in Figure 3.
_______________________________________
Figure 3: Sample Lesson
#print
Of course, you can print any file with "cat".
In particular, it is common to first use
"ls" to find the name of a file and then "cat"
to print it. Note the difference between
"ls", which tells you the name of the files,
and "cat", which tells you the contents.
One file in the current directory is named for
a President. Print the file, then type "ready".
#create roosevelt
this file is named roosevelt
and contains three lines of
text.
#copyout
#user
#uncopyout
tail – 3 .ocopy >X1
#cmp X1 roosevelt
#log
#next
3.2b 2
_______________________________________
Lines which begin with # are commands to the learn script interpreter. For example,
#print
causes printing of any text that follows, up to the next line that begins with a sharp.
#print file
prints the contents of file ; it is the same as cat file but has less overhead. Both forms of #print have the
added property that if a lesson is failed, the #print will not be executed the second time through; this
-7-
_____________________________________________________
Figure 4: Another Sample Lesson
#print
What command will move the current line
to the end of the file? Type
"answer COMMAND", where COMMAND is the command.
#copyin
#user
#uncopyin
#match m$
#match .m$
"m$" is easier.
#log
#next
_____________________________________________________
63.1d 10
writes the date, lesson, user name and speed rating, and a success/failure indication on file. The com-
mand
#log
by itself writes the logging information in the logging directory within the learn hierarchy, and is the
normal form.
#next
is followed by a few lines, each with a successor lesson name and an optional speed rating on it. A typ-
ical set might read
25.1a 10
25.2a 5
25.3a 2
indicating that unit 25.1a is a suitable follow-on lesson for students with a speed rating of 10 units,
25.2a for student with speed near 5, and 25.3a for speed near 2. Speed ratings are maintained for each
session with a student; the rating is increased by one each tiee the student gets a lesson right and
decreased by four each time the student gets a lesson wrong. Thus the driver tries to maintain a devel
such that the users get 80% right answers. The maximum rating is limited to 10 afd the minimum to 0.
The initial rating is zero unless the studeft specifies a differeft rating when starting a session.
If the student passes a lesson, a new lesson is sedected and the process repeats. If the student
fails, a false status is returned and the program reverts to the previous lesson and tries another alterna-
tive. If it can not find another alternative, it skips forward a lesson. bye , bye, which causes a graceful
exit from the learn system. Hanging up is the usual novice’s way out.
The lessons may form an arbitrary directed graph, although the present program imposes a limita-
tion on cycles in that it will not present a lesson twice in the same session. If the student is unable to
answer one of the exercises correctly, the driver searches for a previous lesson with a set of alternatives
as successors (following the #next line). From the previous lesson with alternatives one route was taken
earlier; the program simply tries a different one.
It is perfectly possible to write sophisticated scripts that evaluate the student’s speed of response,
or try to estimate the elegance of the answer, or provide detailed analysis of wrong answers. Lesson
writing is so tedious already, however, that most of these abilities are likely to go unused.
The driver program depends heavily on features of UNIX that are not available on many other
operating systems. These include the ease of manipulating files and directories, file redirection, the abil-
ity to use the command interpreter as just another program (even in a pipeline), command status testing
and branching, the ability to catch signals like interrupts, and of course the pipeline mechanism itself.
-9-
Although some parts of learn might be transferable to other systems, some generality will probably be
lost.
A bit of history: The first version of learn had fewer built-in words in the driver program, and
made more use of the facilities of UNIX. For example, file comparison was done by creating a cmp pro-
cess, rather than comparing the two files within learn . Lessons were not stored as text files, but as
archives. There was no concept of the in-line document; even #print had to be followed by a file name.
Thus the initialization for each lesson was to extract the archive into the working directory (typically 4-8
files), then #print the lesson text.
The combination of such things made learn slower. The new version is about 4 or 5 times faster.
Furthermore, it appears even faster to the user because in a typical lesson, the printing of the message
comes first, and file setup with #create can be overlapped with the printng, so that when the program
finishes printing, it is really ready for the user to type at it.
It is also a great advantage to the script maintainer that lessons are now just ordinary text files.
They can be edited without any difficulty, and UNIX text manipulation tools can be applied to them. The
result has been that there is much less resistance to going in and fixing substandard lessons.
5. Conclusions
The following observations can be made about secretaries, typists, and other non-programmers
who have used learn :
(a) A novice must have assistance with the mechanics of communicating with the computer to get
through to the first lesson or two; once the first few lessons are passed people can proceed on their
own.
(b) The terminology used in the first few lessons is obscure to those inexperienced with computers. It
would help if there were a low level reference card for UNIX to supplement the existing program-
mer oriented bulky manual and bulky reference card.
(c) The concept of ‘‘substitutable argument’’ is hard to grasp, and requires help.
(d) They enjoy the system for the most part. Motivation matters a great deal, however.
It takes an hour or two for a novice to get through the script on file handling. The total time for a rea-
sonably intelligent and motivated novice to proceed from ignorance to a reasonable ability to create new
files and manipulate old ones seems to be a few days, with perhaps half of each day spent on the
machine.
The normal way of proceeding has been to have students in the same room with someone who
knows UNIX and the scripts. Thus the student is not brought to a halt by difficult questions. The burden
on the counselor, however, is much lower than that on a teacher of a course. Ideally, the students
should be encouraged to proceed with instruction immediately prior to their actual use of the computer.
They should exercise the scripts on the same computer and the same kind of terminal that they will later
use for their real work, and their first few jobs for the computer should be relatively easy ones. Also,
both training and initial work should take place on days when the UNIX hardware and software are work-
ing reliably. Rarely is all of this possible, but the closer one comes the better the result. For example,
if it is known that the hardware is shaky one day, it is better to attempt to reschedule training for
another one. Students are very frustrated by machine downtime; when nothing is happening, it takes
some sophistication and experience to distinguish an infinite loop, a slow but functioning program, a
program waiting for the user, and a broken machine.*
One disadvantage of training with learn is that students come to depend completely on the CAI
system, and do not try to read manuals or use other learning aids. This is unfortunate, not only because
of the increased demands for completeness and accuracy of the scripts, but because the scripts do not
cover all of the UNIX system. New users should have manuals (appropriate for their level) and read
them; the scripts ought to be altered to recommend suitable documents and urge students to read them.
__________________
* We have even known an expert programmer to decide the computer was broken when he had simply left his terminal
in local mode. Novices have great difficulties with such problems.
- 10 -
There are several other difficulties which are clearly evident. From the student’s viewpoint, the
most serious is that lessons still crop up which simply can’t be passed. Sometimes this is due to poor
explanations, but just as often it is some error in the lesson itself — a botched setup, a missing file, an
invalid test for correctness, or some system facility that doesn’t work on the local system in the same
way it did on the development system. It takes knowledge and a certain healthy arrogance on the part
of the user to recognize that the fault is not his or hers, but the script writer’s. Permitting the student to
get on with the next lesson regardless does alleviate this somewhat, and the logging facilities make it
easy to watch for lessons that no one can pass, but it is still a problem.
The biggest problem with the previous learn was speed (or lack thereof) — it was often excruciat-
ingly slow and made a significant drain on the system. The current version so far does not seem to have
that difficulty, although some scripts, notably eqn , are intrinsically slow. eqn , for example, must do a
lot of work even to print its introductions, let alone check the student responses, but delay is perceptible
in all scripts from time to time.
Another potential problem is that it is possible to break learn inadvertently, by pushing interrupt at
the wrong time, or by removing critical files, or any number of similar slips. The defenses against such
problems have steadily been improved, to the point where most students should not notice difficulties.
Of course, it will always be possible to break learn maliciously, but this is not likely to be a problem.
One area is more fundamental — some UNIX commands are sufficiently global in their effect that
learn currently does not allow them to be executed at all. The most obvious is cd , which changes to
another directory. The prospect of a student who is learning about directories inadvertently moving to
some random directory and removing files has deterred us from even writing lessons on cd , but ulti-
mately lessons on such topics probably should be added.
6. Acknowledgments
We are grateful to all those who have tried learn, for we have benefited greatly from their sugges-
tions and criticisms. In particular, M. E. Bittrich, J. L. Blue, S. I. Feldman, P. A. Fox, and M. J. McAl-
pin have provided substantial feedback. Conversations with E. Z. Rothkopf also provided many of the
ideas in the system. We are also indebted to Don Jackowski for serving as a guinea pig for the second
version, and to Tom Plum for his efforts to improve the C script.
References
1. B. F. Skinner, ‘‘Why We Need Teaching Machines,’’ Harvard Educational Review 31, pp.377-398
(1961).
2. K. Thompson and D. M. Ritchie, UNIX Programmer’s Manual, Bell Laboratories (May 1975). See
section ed (I).
3. B. W. Kernighan, A Tutorial Introduction to the Unix Editor ed, 1974.
4. B. W. Kernighan and D. M. Ritchie, The C Programming Language, Prentice Hall (1978).
Typing Documents on the UNIX System:
Using the – ms Macros with Troff and Nroff
M. E. Lesk
Bell Laboratories
Murray Hill, New Jersey 07974
ABSTRACT
M
M.. E
E.. L
Leesskk
Bell Laboratories
Murray Hill, New Jersey 07974
Introduction. This memorandum describes a package of commands to produce papers using the
troff and nroff formatting programs on the UNIX system. As with other roff -derived programs, text is
prepared interspersed with formatting commands. However, this package, which itself is written in troff
commands, provides higher-level commands than those provided with the basic troff program. The
commands available in this package are listed in Appendix A.
T
Teexxtt.. Type normally, except that instead of indenting for paragraphs, place a line reading ‘‘.PP’’
before each paragraph. This will produce indenting and extra space.
Alternatively, the command .LP that was used here will produce a left-aligned (block) paragraph. The
paragraph spacing can be changed: see below under ‘‘Registers.’’
B
Beeggiinnnniinngg.. For a document with a paper-type cover sheet, the input should start as follows:
[optional overall format .RP – see below]
.TL
Title of document (one or more lines)
.AU
Author(s) (may also be several lines)
.AI
Author’s institution(s)
.AB
Abstract; to be placed on the cover sheet of a paper.
Line length is 5/6 of normal; use .ll here to change.
.AE (abstract end)
text ... (begins with .PP, which see)
To omit some of the standard headings (e.g. no abstract, or no author’s institution) just omit the
corresponding fields and command lines. The word ABSTRACT can be suppressed by writing ‘‘.AB no’’
for ‘‘.AB’’. Several interspersed .AU and .AI lines can be used for multiple authors. The headings are
not compulsory: beginning with a .PP command is perfectly OK and will just start printing an ordinary
paragraph. W Waarrnniinngg:: You can’t just begin a document with a line of text. Some – ms command must
precede any text input. When in doubt, use .LP to get proper initialization, although any of the com-
mands .PP, .LP, .TL, .SH, .NH is good enough. Figure 1 shows the legal arrangement of commands at
the start of a document.
C
Coovveerr SShheeeettss aanndd F
Fiirrsstt P
Paaggeess.. The first line of a document signals the general format of the first
page. In particular, if it is ".RP" a cover sheet with title and abstract is prepared. The default format is
useful for scanning drafts.
In general – ms is arranged so that only one form of a document need be stored, containing all
information; the first command gives the format, and unnecessary items for that format are ignored.
Warning: don’t put extraneous material between the .TL and .AE commands. Processing of the
titling items is special, and other data placed in them may not behave as you expect. Don’t forget that
some – ms command must precede any input text.
P
Paaggee hheeaaddiinnggss.. The – ms macros, by default, will print a page heading containing a page number
(if greater than 1). A default page footer is provided only in nnrrooffff , where the date is used. The user
-2-
can make minor adjustments to the page headings/footings by redefining the strings LH, CH, and RH
which are the left, center and right portions of the page headings, respectively; and the strings LF, CF,
and RF, which are the left, center and right portions of the page footer. For more complex formats, the
user can redefine the macros PT and BT, which are invoked respectively at the top and bottom of each
page. The margins (taken from registers HM and FM for the top and bottom margin respectively) are
normally 1 inch; the page header/footer are in the middle of that space. The user who redefines these
macros should be careful not to change parameters such as point size or font without resetting them to
default values.
M
Muullttii--ccoolluum
mnn ffoorrm
maattss.. If you place the The .NH command also supports more
command ‘‘.2C’’ in your document, the docu- complex numbering schemes. If a numerical
ment will be printed in double column format argument is given, it is taken to be a ‘‘level’’
beginning at that point. This feature is not too number and an appropriate sub-section number
useful in computer terminal output, but is often is generated. Larger level numbers indicate
desirable on the typesetter. The command deeper sub-sections, as in this example:
‘‘.1C’’ will go back to one-column format and
.NH
also skip to a new page. The ‘‘.2C’’ command
Erie-Lackawanna
is actually a special case of the command
.NH 2
.MC [column width [gutter width]] Morris and Essex Division
.NH 3
which makes multiple columns with the
Gladstone Branch
specified column and gutter width; as many
.NH 3
columns as will fit across the page are used.
Montclair Branch
Thus triple, quadruple, ... column pages can be
.NH 2
printed. Whenever the number of columns is
Boonton Line
changed (except going from full width to some
larger number of columns) a new page is started. generates:
H
Heeaaddiinnggss.. To produce a special heading,
there are two commands. If you type 2. Erie-Lackawanna
References
[1] B. W. Kernighan and L. L. Cherry,
Typesetting Mathematics — Users Guide
(2nd edition), Bell Laboratories Comput-
ing Science Report no. 17.
[2] M. E. Lesk, Tbl — A Program to Format
Tables, Bell Laboratories Computing Sci-
ence Report no. 45.
[3] B. W. Kernighan, A Troff Tutorial, Bell
Laboratories, 1976.
[4] J. F. Ossanna, Nroff /Troff Reference
Manual, Bell Laboratories Computing Sci-
ence Report no. 51.
[5] K. Thompson and D. M. Ritchie, UNIX
Programmer’s Manual, Bell Laboratories,
1978.
-7-
Appendix A
List of Commands
1C Return to single column format. LG Increase type size.
2C Start double column format. LP Left aligned block paragraph.
AB Begin abstract.
AE End abstract.
AI Specify author’s institution.
AU Specify author. ND Change or cancel date.
B Begin boldface. NH Specify numbered heading.
DA Provide the date on each page. NL Return to normal type size.
DE End display. PP Begin paragraph.
DS Start display (also CD, LD, ID).
EN End equation. R Return to regular font (usually Roman).
EQ Begin equation. RE End one level of relative indenting.
FE End footnote. RP Use released paper format.
FS Begin footnote. RS Relative indent increased one level.
SG Insert signature line.
I Begin italics. SH Specify section heading.
SM Change to smaller type size.
IP Begin indented paragraph. TL Specify title.
KE Release keep.
KF Begin floating keep. UL Underline one word.
KS Start keep.
Register Names
The following register names are used by – ms internally. Independent use of these names in
one’s own macros may produce incorrect output. Note that no lower case letters are used in any – ms
internal name.
Number registers used in – ms
: DW GW HM IQ LL NA OJ PO T. TV
#T EF H1 HT IR LT NC PD PQ TB VS
1T FL H3 IK KI MM NF PF PX TD YE
AV FM H4 IM L1 MN NS PI RO TN YY
CW FP H5 IP LE MO OI PN ST TQ ZN
RP
TL
AU
AI
AB
AE
NH, SH
PP, LP
text ...
Figure 1
2
C
Coom
mmmaannddss ffoorr a T
TMM
.TM 1978-5b3 99999 99999-11
.ND April 1, 1976
.TL
A Guide to Preparing The Role of the Allen Wrench in Modern
Electronics
Documents with – ms .AU "MH 2G-111" 2345
J. Q. Pencilpusher
.AU "MH 1K-222" 5432
X. Y. Hardwired
M. E. Lesk .AI
.MH
Bell Laboratories August 1978 .OK
Tools
____________________________________________ Design
.AB
This guide gives some simple examples of This abstract should be short enough to
document preparation on Bell Labs computers, fit on a single page cover sheet.
It must attract the reader into sending for
emphasizing the use of the – m mss macro package. It
the complete memorandum.
enormously abbreviates information in .AE
1. T Tyyppiinngg D Dooccuum meennttss oonn U UNNIIXX aanndd G GC CO
OSS,, by .CS 10 2 12 5 6 7
M. E. Lesk; .NH
2. T Tyyppeesseettttiinngg MMaatthheem maattiiccss – U Usseerr’’ss G Guuiiddee,, by Introduction.
B. W. Kernighan and L. L. Cherry; and .PP
3. T Tbbll – A P Prrooggrraam
m ttoo FFoorrm maatt T Taabblleess,, by M. E. Now the first paragraph of actual text ...
Lesk. ...
These memos are all included in the U UN NIIX
X Last line of text.
P
Prrooggrraam
mm meerr’’ss M Maannuuaall,, V
Voolluum mee 22.. The new user .SG MH-1234-JQP/XYH-unix
.NH
should also have A T Tuuttoorriiaall IInnttrroodduuccttiioonn ttoo tthhee
References ...
U
UNNIIXXT Teexxtt E
Eddiittoorr,, by B. W. Kernighan.
For more detailed information, read A Addvvaanncceedd Commands not needed in a particular format are ignored.
____________________________________________________________________________
E
Eddiittiinngg oonn U
UN NIIX
X and A T Trrooffff T
Tuuttoorriiaall,, by B. W.
Kernighan, and (for experts) N Nrrooffff //T
Trrooffff R
Reeffeerreennccee
M
Maannuuaall by J. F. Ossanna. Information on related Bell Laboratories Cover Sheet for TM
commands is found (for UNIX users) in U UNNIIX X ffoorr
________________________________________________________________________
B
Beeggiinnnneerrss by B. W. Kernighan and the U UN NIIX X This information is for employees of Bell Laboratories. (GEI 13.9-3)
________________________________________________________________________
P
Prrooggrraam mmmeerr’’ss M
Maannuuaall by K. Thompson and D. M.
Ritchie.
Title- TThhee RRoollee ooff tthhee AAlllleenn W
Wrreenncchh Apprriill 11,, 11997766
Date- A
i
inn M
Mo oddeer
rnn E
Elleeccttr
roon
ni ic
css
TM- 1 1997788--55bb33
Contents
Other Keywords- T Toooollss
D
Deessiiggnn
A TM . . . . . . . . . . . . . . . . . . . . 2
A released paper . . . . . . . . . . . . . . 3
An internal memo, and headings . . . . . 4
Author Location Ext. Charging Case- 99999
Lists, displays, and footnotes . . . . . . . 5 J
J.. Q
Q.. P
Pe en
ncciil
lppu
usshheer
r M
MH H 2
2GG--111111 2
2334455 Filing Case- 99999a
Indents, keeps, and double column . . . . 6 X
X.. Y
Y.. H Haarrddw
wiirreedd M
MH H 11K K--222222 55443322
Equations and registers . . . . . . . . . . 7
ABSTRACT
Tables and usage . . . . . . . . . . . . . . 8
This abstract should be short enough to fit
Throughout the examples, input is shown in on a single page cover sheet. It must attract the
reader into sending for the complete memoran-
this Helvetica sans serif font dum.
while the resulting output is shown in
this Times Roman font.
______________________________________________________
UNIX Document no. 1111 Pages Text 10 Other 2 Total 12
No. Figures 5 No. Tables 6 No. Refs. 7
______________________________________________________
E-1932-U (6-73) SEE REVERSE SIDE FOR DISTRIBUTION LIST
____________________________________________________________________________
3 4
AR
Reelleeaasseedd P
Paappeerr w
wiitthh M
Maatthheem
maattiiccss A
Ann IInntteerrnnaall M
Meem
moorraanndduum
m
.EQ .IM
delim $$ .ND January 24, 1956
.EN .TL
.RP The 1956 Consent Decree
.AU
... (as for a TM) Able, Baker &
Charley, Attys.
.CS 10 2 12 5 6 7 .PP
.NH Plaintiff, United States of America, having filed
Introduction its complaint herein on January 14, 1949; the
.PP defendants having appeared and filed their
The solution to the torque handle equation answer to such complaint denying the
.EQ (1) substantive allegations thereof; and the parties,
sum from 0 to inf F ( x sub i ) = G ( x ) by their attorneys, ...
.EN __________________________________________________________
is found with the transformation $ x = rho over
theta $ where $ rho = G prime (x) $ and $theta$
is derived ...
B
Beel
lll L
Laabbo
or ra
atto
orri
iees
s
__________________________________________________________
Subject: T Thhee 11995566 C Coonnsseenntt D
Deeccrreeee date: JJaan nuuaarryy 2244,, 11995566
from: A Abbllee,, BBaakkeerr &
C
Chhaarrlleeyy,, AAttttyyss..
T
Thhee RRoollee ooff tthhee A
Alllleenn W Wrreenncchh
iinn M
Mooddeerrnn E Elleeccttrroonniiccss
Plaintiff, United States of America, having filed its complaint
herein on January 14, 1949; the defendants having appeared and
J. Q. Pencilpusher filed their answer to such complaint denying the substantive alle-
gations thereof; and the parties, by their attorneys, having
X. Y. Hardwired severally consented to the entry of this Final Judgment without
trial or adjudication of any issues of fact or law herein and
Bell Laboratories without this Final Judgment constituting any evidence or admis-
Murray Hill, New Jersey 07974 sion by any party in respect of any such issues;
Now, therefore before any testimony has been taken herein,
and without trial or adjudication of any issue of fact or law
ABSTRACT
herein, and upon the consent of all parties hereto, it is hereby
This abstract should be short enough to fit on a sin- Ordered, adjudged and decreed as follows:
gle page cover sheet. It must attract the reader into II.. [[SShheerrm
maann A Acctt]]
sending for the complete memorandum.
This Court has jurisdiction of the subject matter herein and of
all the parties hereto. The complaint states a claim upon which
relief may be granted against each of the defendants under Sec-
tions 1, 2 and 3 of the Act of Congress of July 2, 1890, entitled
‘‘An act to protect trade and commerce against unlawful restraints
and monopolies,’’ commonly known as the Sherman Act, as
April 1, 1976 amended.
__________________________________________________________ IIII.. [[D
Deefifinniittiioonnss]]
__________________________________________________________
For the purposes of this Final Judgment:
(a) ‘‘Western’’ shall mean the defendant Western Electric
Company, Incorporated.
T
Thhee RRoollee ooff tthhee A
Alllleenn W Wrreenncchh __________________________________________________________
iinn M
Mooddeerrnn E Elleeccttrroonniiccss
J. Q. Pencilpusher Other formats possible (specify before .TL) are: .MR
(‘‘memo for record’’), .MF (‘‘memo for file’’), .EG
X. Y. Hardwired (‘‘engineer’s notes’’) and .TR (Computing Science Tech.
Report).
Bell Laboratories
Murray Hill, New Jersey 07974
H
Heeaaddiinnggss
.NH .SH
11.. IInnttrroodduuccttiioonn Introduction. Appendix I
The solution to the torque handle equation
∞ .PP .PP
Σ F (xi )=G (x ) (1) text text text text text text
0
is found with the transformation x = _ρ_ where ρ=G ′(x ) and θ is 11.. IInnttrroodduuccttiioonn A
Appppeennddiixx I
θ
derived from well-known principles. text text text text text text
__________________________________________________________
5 6
AS
Siim
mppllee L
Liisstt M
Muullttiippllee IInnddeennttss
.IP 1. This is ordinary text to point out
J. Pencilpusher and X. Hardwired, the margins of the page.
.I .IP 1.
A New Kind of Set Screw, First level item
.R .RS
Proc. IEEE .IP a)
.B 75 Second level.
(1976), 23-255. .IP b)
.IP 2. Continued here with another second
H. Nails and R. Irons, level item, but somewhat longer.
.I .RE
Fasteners for Printed Circuit Boards, .IP 2.
.R Return to previous value of the
Proc. ASME indenting at this point.
.B 23 .IP 3.
(1974), 23-24. Another
.LP (terminates list) line.
1. J. Pencilpusher and X. Hardwired, A New Kind of This is ordinary text to point out the margins of the page.
Set Screw, Proc. IEEE 75 (1976), 23-255. 1. First level item
2. H. Nails and R. Irons, Fasteners for Printed Circuit a) Second level.
Boards, Proc. ASME 23 (1974), 23-24. b) Continued here with another second level item,
but somewhat longer.
2. Return to previous value of the indenting at this
D
Diissppllaayyss point.
3. Another line.
text text text text text text
.DS
and now K
Keeeeppss
for something
completely different Lines bracketed by the following commands are kept
.DE together, and will appear entirely on one page:
text text text text text text .KS not moved .KF may float
.KE through text .KE in text
hoboken harrison newark roseville avenue grove street
east orange brick church orange highland avenue moun-
tain station south orange maplewood millburn short hills D
Doouubbllee C
Coolluum
mnn
summit new providence
.TL
and now
The Declaration of Independence
for something
.2C
completely different
.PP
murray hill berkeley heights gillette stirling millington When in the course of human events, it becomes
lyons basking ridge bernardsville far hills peapack glad- necessary for one people to dissolve the political
stone bonds which have connected them with another, and
to assume among the powers of the earth the
Options: .DS L: left-adjust; .DS C: line-by-line center;
separate and equal station to which the laws of
.DS B: make block, then center.
Nature and of Nature’s God entitle them, a decent
respect to the opinions of
F
Foooottnnootteess
The Declaration of Independence
Among the most important occupants
When in the course of We hold these truths to
of the workbench are the long-nosed pliers.
human events, it becomes be self-evident, that all
Without these basic tools*
necessary for one people to men are created equal, that
.FS
dissolve the political bonds they are endowed by their
* As first shown by Tiger & Leopard
which have connected creator with certain
(1975).
them with another, and to unalienable rights, that
.FE
assume among the powers among these are life, liber-
few assemblies could be completed. They may
of the earth the separate ty, and the pursuit of hap-
lack the popular appeal of the sledgehammer
and equal station to which piness. That to secure
the laws of Nature and of these rights, governments
Among the most important occupants of the workbench Nature’s God entitle them, are instituted among men,
are the long-nosed pliers. Without these basic tools* few a decent respect to the
assemblies could be completed. They may lack the popu- opinions of mankind re-
lar appeal of the sledgehammer quires that they should de-
________________ clare the causes which im-
* As first shown by Tiger & Leopard (1975). pel them to the separation.
7 8
E
Eqquuaattiioonnss T
Taabblleess
A displayed equation is marked .TS ( T indicates a tab)
with an equation number at the right margin allbox; _____________________
by adding an argument to the EQ line: css _____________________
AT&T Common Stock
.EQ (1.3) ccc _____________________
Year Price Dividend
x sup 2 over a sup 2 ˜=˜ sqrt {p z sup 2 +qz+r} n n n. 1971 41-54 $2.60
_____________________
.EN AT&T Common Stock 2 41-54 2.70
_____________________
Year T Price T Dividend
A displayed equation is marked with an equation number 1971 T 41-54 T $2.60 _____________________
3 46-55 2.87
_____________________
4 40-53 3.24
at the right margin by adding an argument to the EQ line: 2 T 41-54 T 2.70 _____________________
3 T 46-55 T 2.87 5 45-52 3.40
2
_x__ = √
pz 2+qz +r (1.3) 4 T 40-53 T 3.24 6 51-59 .95*
_____________________
a2
5 T 45-52 T 3.40 * (first quarter only)
.EQ I (2.2a) 6 T 51-59 T .95*
bold V bar sub nu˜=˜left [ pile {a above b above .TE
c } right ] + left [ matrix { col { A(11) above . * (first quarter only)
above . } col { . above . above .} col {. above . The meanings of the key-letters describing the alignment
above A(33) }} right ] cdot left [ pile { alpha of each entry are:
above beta above gamma } right ] c center n numerical
.EN r right-adjust a subcolumn
l left-adjust s spanned
a A (11) . . α The global table options are center, expand, box,
V ν = b + .
V . . . β (2.2a)
c . . A (33) γ doublebox, allbox, tab (xx ) and linesize (nn ).
ABSTRACT
This paper describes the design and implementation of a system for typesetting mathemat-
ics. The language has been designed to be easy to learn and to use by people (for example,
secretaries and mathematical typists) who know neither mathematics nor typesetting. Experience
indicates that the language can be learned in an hour or so, for it has few rules and fewer e