UNIX
IN A NUTSHELL
,TITLE.12845 Page i Tuesday, August 22, 2006 4:51 PM
Other resources from O’Reilly
Related titles Classic Shell Scripting
Effective awk
Programming
Essential CVS
Essential System
Administration
GDB Pocket Reference
Learning GNU Emacs
Learning the bash Shell
Learning the Korn Shell
Learning the vi Editor
Linux in a Nutshell
Mac OS X Tiger for Unix
Geeks
Managing Projects with
GNU Make
Running Linux
sed and awk Pocket
Reference
TCP/IP Network
Administration
The Complete FreeBSD
Unix Power Tools
Using csh & tcsh
Version Control with
Subversion
oreilly.com oreilly.com is more than a complete catalog of O’Reilly
books. You'll also find links to news, events, articles,
weblogs, sample chapters, and code examples.
Conferences O’Reilly brings diverse innovators together to nurture the
ideas that spark revolutionary industries. We specialize in
documenting the latest tools and systems, translating the
innovator’s knowledge into useful skills for those in the
trenches. Visit conferences.oreilly.com for our upcoming
events.
Safari Bookshelf (safari.oreilly.com) is the premier online
reference library for programmers and IT professionals.
Conduct searches across more than 1,000 books. Sub-
scribers can zero in on answers to time-critical questions
in a matter of seconds. Read the books on your Book-
shelf from cover to cover or simply flip to the page you
need. Try it today for free.
,TITLE.12845 Page ii Tuesday, August 22, 2006 4:51 PM
Fourth Edition
UNIX
IN A NUTSHELL
Arnold Robbins
Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo
,TITLE.12845 Page iii Tuesday, August 22, 2006 4:51 PM
Unix in a Nutshell, Fourth Edition
by Arnold Robbins
Copyright © 2006, 1999, 1992, 1989 O’Reilly Media, Inc. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online
editions are also available for most titles (safari.oreilly.com). For more information, contact
our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com.
Editor: Mike Loukides
Production Editor: Colleen Gorman
Cover Designer: Edie Freedman
Interior Designer: David Futato
Back Cover Illustration: J.D. “Illiad” Frazer
Printing History:
May 1989: First Edition.
June 1992: Second Edition.
August 1999: Third Edition.
October 2005: Fourth Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered
trademarks of O’Reilly Media, Inc. The In a Nutshell series designations, Unix in a Nutshell,
the image of a tarsier, and related trade dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in this book, and O’Reilly Media,
Inc. was aware of a trademark claim, the designations have been printed in caps or initial
caps. UNIX is a registered trademark of The Open Group.
While every precaution has been taken in the preparation of this book, the publisher and
author assume no responsibility for errors or omissions, or for damages resulting from the use
of the information contained herein.
ISBN: 0-596-10029-9
[M] [8/06]
,COPYRIGHT.12974 Page iv Tuesday, August 22, 2006 4:52 PM
To my wife, Miriam. May our dreams continue to come true.
To my children, Chana, Rivka, Nachum, and Malka.
To the memory of Frank Willison.
vii
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
Chapter 1
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Part I. Commands and Shells
1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Unix in the 21st Century 3
Obtaining Compilers 5
Building Software 6
What’s in the Quick Reference 7
Beginner’s Guide 8
Solaris: Standard Compliant Programs 11
2. Unix Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Introduction 13
Alphabetical Summary of Common Commands 15
Alphabetical Summary of Solaris Commands 241
Alphabetical Summary of GNU/Linux Commands 260
Alphabetical Summary of Mac OS X Commands 304
Alphabetical Summary of Java Commands 321
3. The Unix Shell: An Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Introduction to the Shell 341
Purpose of the Shell 342
Shell Flavors 343
viii | Table of Contents
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
Shell Source Code URLs 344
Common Features 344
Differing Features 345
4. The Bash and Korn Shells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Overview of Features 348
Invoking the Shell 349
Syntax 350
Functions 357
Variables 358
Arithmetic Expressions 366
Command History 368
Job Control 372
Command Execution 372
Restricted Shells 373
Built-in Commands (Bash and Korn Shells) 374
5. tcsh: An Extended C Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Overview of Features 417
Invoking the Shell 418
Syntax 419
Variables 423
Expressions 435
Command History 438
Command-Line Manipulation 442
Job Control 445
Built-in Commands 446
6. Package Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
Linux Package Management 467
The Red Hat Package Manager 470
Yum: Yellowdog Updater Modified 484
up2date: Red Hat Update Agent 489
The Debian Package Manager 492
Mac OS X Package Management 520
Solaris Package Management 521
Table of Contents | ix
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
Part II. Text Editing and Processing
7. Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535
Filenames Versus Patterns 535
Metacharacters 536
Metacharacters, Listed by Unix Program 538
Examples of Searching 539
8. The Emacs Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Conceptual Overview 543
Command-Line Syntax 545
Summary of Commands by Group 546
Summary of Commands by Key 552
Summary of Commands by Name 555
9. The vi, ex, and vim Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
Conceptual Overview 561
Command-Line Syntax 562
Review of vi Operations 565
vi Commands 567
vi Configuration 574
ex Basics 579
Alphabetical Summary of ex Commands 580
10. The sed Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
Conceptual Overview 595
Command-Line Syntax 596
Syntax of sed Commands 598
Group Summary of sed Commands 600
Alphabetical Summary of sed Commands 601
11. The awk Programming Language . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
Conceptual Overview 611
Command-Line Syntax 613
Patterns and Procedures 614
Built-in Variables 616
Operators 617
Variable and Array Assignment 618
User-Defined Functions 619
Gawk-Specific Features 620
x | Table of Contents
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
Implementation Limits 622
Group Listing of awk Functions and Commands 623
Alphabetical Summary of awk Functions and Commands 623
Output Redirections 632
Source Code 634
Part III. Software Development
12. Source Code Management: An Overview . . . . . . . . . . . . . . . . . . . . . 637
Introduction and Terminology 637
Usage Models 639
Unix Source Code Management Systems 640
Other Source Code Management Systems 641
13. The Revision Control System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
Overview of Commands 643
Basic Operation 644
General RCS Specifications 645
Alphabetical Summary of Commands 649
14. The Concurrent Versions System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659
Conceptual Overview 659
Command-Line Syntax and Options 661
Dot Files 664
Environment Variables 665
Keywords and Keyword Modes 667
Dates 669
CVSROOT Variables 672
Alphabetical Summary of Commands 674
15. The Subversion Version Control System . . . . . . . . . . . . . . . . . . . . . . . 697
Conceptual Overview 697
Obtaining Subversion 702
Using Subversion: A Quick Tour 704
The Subversion Command Line Client: svn 706
Repository Administration: svnadmin 733
Examining the Repository: svnlook 737
Providing Remote Access: svnserve 742
Other Subversion Components 743
Table of Contents | xi
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
16. The GNU make Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745
Conceptual Overview 745
Command-Line Syntax 746
Makefile Lines 749
Macros 754
Special Target Names 761
Writing Command Lines 762
17. The GDB Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765
Conceptual Overview 765
Command-Line Syntax 768
Initialization Files 770
GDB Expressions 771
The GDB Text User Interface 773
Group Listing of GDB Commands 773
Summary of set and show Commands 777
Summary of the info Command 788
Alphabetical Summary of GDB Commands 790
18. Writing Manual Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813
Introduction 813
Overview of nroff/troff 814
Alphabetical Summary of man Macros 819
Predefined Strings 823
Internal Names 823
Sample Document 823
Part IV. References
ISO 8859-1 (Latin-1) Character Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829
Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 837
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849
xiii
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
Chapter 2
Preface
The fourth edition of Unix in a Nutshell brings the book into the 21st century.
The term “UNIX” is a registered trademark of The Open Group. It is used for
branding systems as compliant with the various standards that collectively define
the behavior of a modern Unix system. More informally though, many systems in
use today are Unix work-alikes, even though their source code base was devel-
oped independently from the original Unix systems.
Thus, the goal of this edition to present the broader state of Unix in today’s world.
In particular, it’s important to cover both the commercial variants, and those where
source code for the system and the utilities are freely available. To this end, we have
chosen to cover these systems, which are representative of “Unix” today:
Solaris 10
Solaris is the most popular commercial system based on the original Unix
System V code base.
GNU/Linux
GNU/Linux systems have gained a huge foothold in the commercial market-
place. While currently used most heavily for back-end servers, GNU/Linux is
also starting to gain ground in the desktop market.
Mac OS X
Apple’s rewrite of their operating system has a core based on Mach and
various BSD technologies. The command set is derived from FreeBSD. Thus,
besides having an exciting user interface, Mac OS X is representative of the
BSD strain of free Unix-like systems.
The commands covered by the current POSIX standard form the core of our presen-
tation. Each specific system has commands that are unique to it; these are covered
too. Finally, many important and useful utilities are distributed as Free or Open
Source software on the Internet. We have done our best to cover those as well,
including presenting the Internet URL from which you can download the source
code, in case your particular system doesn’t include that utility in its distribution.
xiv | Preface
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
This edition has the following new features:
• Covers Solaris 10, the latest version of the SVR4-based operating system from
Sun Microsystems,* GNU/Linux, and Mac OS X.
• Chapter 2, Unix Commands, has been heavily reorganized and revised, in
order to cover the three systems.
• Chapter 3, The Unix Shell: An Overview, has been reworked, now covering
Bash,† ksh93, and tcsh.
• Chapter 4, The Bash and Korn Shells, now covers the popular Bash shell,
along with the 1988 and 1993 versions of ksh. Coverage of the vanilla Bourne
shell has been dropped.
• Chapter 5, tcsh: An Extended C Shell, now covers the widely-used tcsh shell
instead of the original Berkeley csh.
• Chapter 6, Package Management, is new. It covers package management pro-
grams, which are used for program installation on popular GNU/Linux sys-
tems. It also describes similar facilities for Solaris and Mac OS X.
• Chapter 8, The Emacs Editor, now covers GNU Emacs Version 21.
• Chapter 9, The vi, ex, and vim Editors, now contains merged coverage of the
vi and ex text editors. Important commands and features from the popular
vim editor are also included.
• Chapter 10, The sed Editor, now includes coverage of GNU sed.
• The coverage of awk in Chapter 11, The awk Programming Language, has
been updated as well, dropping separate coverage of the original, “old” awk.
• Chapter 12, Source Code Management: An Overview, which provides an
introduction to source code management systems, has been added.
• Chapter 14, The Concurrent Versions System, on CVS, has been added.
• Chapter 15, The Subversion Version Control System, on the Subversion ver-
sion control system, is brand new.
• Chapter 16, The GNU make Utility, has been revised to focus on GNU Make.
• Chapter 17, The GDB Debugger, on the GDB debugger, is brand new.
As time marches on, once-popular or necessary commands fall into disuse. Thus,
with the exception of Chapter 18, which describes how to write a manual page, all
the material on the venerable troff text formatting suite has been removed from
the book. We have also removed the previous edition’s material on SCCS and on
obsolete commands.
Audience
This book should be of interest to Unix users and Unix programmers, as well as to
anyone (such as a system administrator) who might offer direct support to users
* The version used for this book was for Intel x86–based systems.
† Because the Free Software Foundation treats “Bash” and “Emacs” as proper nouns, we do too,
here and throughout the book.
Preface | xv
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
and programmers. The presentation is geared mainly toward people who are
already familiar with the Unix system; that is, you know what you want to do,
and you even have some idea how to do it. You just need a reminder about the
details. For example, if you want to remove the third field from a database, you
might think, “I know I can use the cut command, but what are the options?” In
many cases, specific examples are provided to show how a command is used.
We have purposely chosen to omit system administration commands. System
administration is a complicated topic in its own right, and the Bibliography lists
several good books on this important subject.
This reference might also help people who are familiar with some aspects of Unix
but not with others. Many chapters include an overview of the particular topic.
While this isn’t meant to be comprehensive, it’s usually sufficient to get you
started in unfamiliar territory.
Finally, if you’re new to the Unix operating system, and you’re feeling bold, you
might appreciate this book as a quick tour of what Unix has to offer. The
“Beginner’s Guide” section in Chapter 1 can point you to the most useful
commands, and you’ll find brief examples of how to use them, but take note: this
book should not be used in place of a good beginner’s tutorial on Unix. (You might
try Learning the Unix Operating System for that.) This reference should be a supple-
ment, not a substitute. (There are references throughout the text to other relevant
O’Reilly books that will help you learn the subject matter under discussion; you
may be better off detouring to those books first. Also, see the Bibliography.)
Scope of This Book
Unix in a Nutshell, Fourth Edition, is divided into four parts:
• Part I (Chapters 1 through 6) describes the syntax and options for Unix com-
mands and for the Bash, Korn, and tcsh shells. Part I also covers package
management.
• Part II (Chapters 7 through 11) presents various editing tools and describes
their command sets (alphabetically and by group). Part II begins with a review
of pattern matching, including examples geared toward specific editors.
• Part III (Chapters 12 through 18) summarizes the Unix utilities for software
development—RCS, CVS, Subversion, make and GDB. It also covers, in brief,
what you need to know to write a manual page for your programs.
• Part IV contains a table of ISO Latin-1 characters and equivalent values (ISO
8859-1 (Latin-1) Character Set) and a Bibliography of Unix books.
Conventions
This book follows certain typographic conventions, outlined below:
Constant width
is used for directory names, filenames, commands, program names, func-
tions, and options. All terms shown in constant width are typed literally. It is
also used to show the contents of files or the output from commands.
xvi | Preface
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
Constant width italic
is used in syntax and command summaries to show generic text; these should
be replaced with user-supplied values.
Constant width bold
is used in examples to show text that should be typed literally by the user.
Italic
is used to show generic arguments and options; these should be replaced with
user-supplied values. Italic is also used to indicate URLs, macro package
names, library names, comments in examples, and the first mention of terms.
%, $, #
are used in some examples as the C shell prompt (%) and as the Bash, Bourne
or Korn shell prompts ($). # is the prompt for the root user.
?, >
are used in some examples as the C shell secondary prompt (?) and as the
Bash, Bourne or Korn shell secondary prompts (>).
❑,➔
are used in some examples to represent the space and tab characters respec-
tively. This is particularly necessary for the examples in the chapters on text
editing.
program(N)
indicates the “manpage” for program in section N of the online manual. For
example, echo(1) means the entry for the echo command.
[ ]
surround optional elements in a description of syntax. (The brackets them-
selves should never be typed.) Note that many commands show the argument
[files]. If a filename is omitted, standard input (usually the keyboard) is
assumed. End keyboard input with an end-of-file character.
EOF
indicates the end-of-file character (normally CTRL-D).
^x, CTRL-x
indicates a “control character,” typed by holding down the Control key and
the x key for any key x.
|
is used in syntax descriptions to separate items for which only one alterna-
tive may be chosen at a time.
A final word about syntax. In many cases, the space between an option and its
argument can be omitted. In other cases, the spacing (or lack of spacing) must be
followed strictly. For example, -wn (no intervening space) might be interpreted
differently from -w n. It’s important to notice the spacing used in option syntax.
This icon signifies a tip, sugges