Vous êtes sur la page 1sur 197

Shell Programming

in Unix, Linux
and OS X
Fourth Edition
Shell Programming
in Unix, Linux
and OS X
Fourth Edition

Stephen G. Kochan
Patrick Wood

800 East 96th Street, Indianapolis, Indiana 46240


Shell Programming in Unix, Linux and OS X, Fourth Edition Editor
Copyright © 2017 by Pearson Education, Inc. Mark Taber

All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or Copy Editor
transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, Larry Sulky
without written permission from the publisher. No patent liability is assumed with respect to Technical Editor
the use of the information contained herein. Although every precaution has been taken in Brian Tiemann
the preparation of this book, the publisher and author assume no responsibility for errors or
omissions. Nor is any liability assumed for damages resulting from the use of the informa- Designer
tion contained herein. Chuti Prasertsith
Page Layout
ISBN-13: 978-0-13-4449600-9
codeMantra
ISBN-10: 0-13-449600-0
Printed in the United States of America
First Printing: August 2016
The Library of Congress Control Number is on file.

Trademarks
All terms mentioned in this book that are known to be trademarks or service marks have
been appropriately capitalized. The publisher cannot attest to the accuracy of this
information. Use of a term in this book should not be regarded as affecting the validity of
any trademark or service mark.

Warning and Disclaimer


Every effort has been made to make this book as complete and as accurate as possible, but
no warranty or fitness is implied. The information provided is on an “as is” basis. The author
and the publisher shall have neither liability nor responsibility to any person or entity with
respect to any loss or damages arising from the information contained in this book.

Special Sales
For information about buying this title in bulk quantities, or for special sales opportunities
(which may include electronic versions; custom cover designs; and content particular to
your business, training goals, marketing focus, or branding interests), please contact our
corporate sales department at corpsales@pearsoned.com or (800) 382-3419.
For government sales inquiries, please contact
governmentsales@pearsoned.com
For questions about sales outside the U.S., please contact
international@pearsoned.com
Contents at a Glance
Introduction 1

1 A Quick Review of the Basics 5

2 What Is the Shell? 39

3 Tools of the Trade 51

4 And Away We Go 93

5 Can I Quote You on That? 105

6 Passing Arguments 121

7 Decisions, Decisions 131

8 'Round and 'Round She Goes 163

9 Reading and Printing Data 185

10 Your Environment 209

11 More on Parameters 239

12 Loose Ends 255

13 Rolo Revisited 273

14 Interactive and Nonstandard Shell Features 28


9

A Shell Summary 321

B For More Information 359

Index 363
Table of
Contents

Introduction 1
How This Book Is Organized 2
Accessing the Free Web Edition 3

1 A Quick Review of the Basics 5


Some Basic Commands 5
Displaying the Date and Time: The date Command 5
Finding Out Who’s Logged In: The who Command 5
Echoing Characters: The echo Command 6
Working with Files 6
Listing Files: The ls Command 7
Displaying the Contents of a File: The cat Command 7
Counting the Number of Words in a File: The wc Command 7
Command Options 8
Making a Copy of a File: The cp Command 8
Renaming a File: The mv Command 8
Removing a File: The rm Command 9
Working with Directories 9
The Home Directory and Pathnames 10
Displaying Your Working Directory: The pwd Command 12
Changing Directories: The cd Command 12
More on the ls Command 15
Creating a Directory: The mkdir Command 17
Copying a File from One Directory to Another 18
Moving Files Between Directories 19
Linking Files: The ln Command 20
Removing a Directory: The rmdir Command 23
Filename Substitution 24
The Asterisk 24
Matching Single Characters 25
Filename Nuances 27
Spaces in Filenames 27
Other Weird Characters 28
Standard Input/Output, and I/O Redirection 28
Standard Input and Standard Output 28
Contents vi

Output Redirection 30
Input Redirection 32
Pipes 33
Filters 35
Standard Error 35
More on Commands 36
Typing More Than One Command on a Line 36
Sending a Command to the Background 36
The ps Command 37
Command Summary 37

2 What Is the Shell? 39


The Kernel and the Utilities 39
The Login Shell 40
Typing Commands to the Shell 43
The Shell’s Responsibilities 44
Program Execution 45
Variable and Filename Substitution 47
I/O Redirection 48
Hooking up a Pipeline 49
Environment Control 49
Interpreted Programming Language 50

3 Tools of the Trade 51


Regular Expressions 51
Matching Any Character: The Period (.) 51
Matching the Beginning of the Line: The Caret ( ^) 53
Matching the End of the Line: The Dollar Sign $ 53
Matching a Character Set: The [...] Construct 55
Matching Zero or More Characters: The Asterisk ( *) 57
Matching a Precise Number of Subpatterns: \{...\} 59
Saving Matched Characters: \(...\) 61
cut 64
The -d and -f Options 66
paste 68
The -d Option 69
The -s Option 70
vii Contents

sed 70
The -n Option 72
Deleting Lines 73
tr 74
The -s Option 76
The -d Option 77
grep 78
Regular Expressions and grep 81
The -v Option 82
The -l Option 82
The -n Option 83
sort 84
The -u Option 84
The -r Option 85
The -o Option 85
The -n Option 86
Skipping Fields 87
The -t Option 87
Other Options 88
uniq 88
The -d Option 89
Other Options 90

4 And Away We Go 93
Command Files 93
Comments 96
Variables 97
Displaying the Values of Variables 98
Undefined Variables Have the Null Value 100
Filename Substitution and Variables 101
The ${variable} Construct 102
Built-in Integer Arithmetic 103

5 Can I Quote You on That? 105


The Single Quote 105
The Double Quote 109
The Backslash 111
Contents ix

Using the Backslash for Continuing Lines 112


The Backslash Inside Double Quotes 112
Command Substitution 114
The Back Quote 114
The $(...) Construct 115
The expr Command 119

6 Passing Arguments 121


The $# Variable 122
The $* Variable 123
A Program to Look Up Someone in the Phone Book 124
A Program to Add Someone to the Phone Book 125
A Program to Remove Someone from the Phone Book 127
${n} 128
The shift Command 128

7 Decisions, Decisions 131


Exit Status 131
The $? Variable 132
The test Command 135
String Operators 135
An Alternative Format for test 139
Integer Operators 140
File Operators 142
The Logical Negation Operator ! 143
The Logical AND Operator -a 143
Parentheses 144
The Logical OR Operator -o 144
The else Construct 145
The exit Command 147
A Second Look at the rem Program 147
The elif Construct 148
Yet Another Version of rem 151
The case Command 153
Special Pattern-Matching Characters 155
The -x Option for Debugging Programs 157
Back to the case 159
x Contents

The Null Command : 160


The && and || Constructs 161

8 'Round and 'Round She Goes 163


The for Command 163
The $@ Variable 166
The for Without the List 167
The while Command 168
The until Command 170
More on Loops 174
Breaking Out of a Loop 174
Skipping the Remaining Commands in a Loop 176
Executing a Loop in the Background 177
I/O Redirection on a Loop 177
Piping Data into and out of a Loop 178
Typing a Loop on One Line 179
The getopts Command 180

9 Reading and Printing Data 185


The read Command 185
A Program to Copy Files 185
Special echo Escape Characters 187
An Improved Version of mycp 188
A Final Version of mycp 190
A Menu-Driven Phone Program 193
The $$ Variable and Temporary Files 198
The Exit Status from read 199
The printf Command 202

10 Your Environment 209


Local Variables 209
Subshells 210
Exported Variables 211
export -p 215
PS1 and PS2 216
HOME 217
PATH 217
Contents xi

Your Current Directory 225


CDPATH 226
More on Subshells 227
The .Command 227
The exec Command 230
The (...) and { ...; } Constructs 231
Another Way to Pass Variables to a Subshell 234
Your .profile File 235
The TERM Variable 236
The TZ Variable 237

11 More on Parameters 239


Parameter Substitution 239
${parameter} 239
${parameter:-value} 240
${parameter:=value} 241
${parameter:?value} 241
${parameter:+value} 242
Pattern Matching Constructs 242
${#variable} 244
The $0 Variable 245
The set Command 246
The -x Option 246
set with No Arguments 247
Using set to Reassign Positional Parameters 247
The -- Option 248
Other Options to set 251
The IFS Variable 251
The readonly Command 254
The unset Command 254

12 Loose Ends 255


The eval Command 255
The wait Command 257
The $! Variable 257
The trap Command 258
trap with No Arguments 259
xii Contents

Ignoring Signals 260


Resetting Traps 261
More on I/O 261
<&- and >&- 262
In-line Input Redirection 262
Shell Archives 264
Functions 268
Removing a Function Definition 271
The return Command 271
The type Command 271

13 Rolo Revisited 273


Data Formatting Considerations 273
rolo 274
add 277
lu 278
display 278
rem 280
change 281
listall 283
Sample Output 284

14 Interactive and Nonstandard Shell Features 289


Getting the Right Shell 289
The ENV File 290
Command-Line Editing 291
Command History 292
The vi Line Edit Mode 292
Accessing Commands from Your History 294
The emacs Line Edit Mode 296
Accessing Commands from Your History 298
Other Ways to Access Your History 300
The history Command 300
The fc Command 301
The r Command 301
Functions 303
Local Variables 303
Automatically Loaded Functions 303
Contents xii

Integer Arithmetic 303


Integer Types 304
Numbers in Different Bases 305
The alias Command 307
Removing Aliases 309
Arrays 309
Job Control 315
Stopped Jobs and the fg and bg Commands 316
Miscellaneous Features 317
Other Features of the cd Command 317
Tilde Substitution 318
Order of Search 319
Compatibility Summary 319

A Shell Summary 321


Startup 321
Commands 321
Comments 322
Parameters and Variables 322
Shell Variables 322
Positional Parameters 322
Special Parameters 323
Parameter Substitution 324
Command Re-entry 326
The fc Command 326
vi Line Edit Mode 326
Quoting 329
Tilde Substitution 329
Arithmetic Expressions 330
Filename Substitution 331
I/O Redirection 331
Exported Variables and Subshell Execution 332
The (...) Construct 332
The { ...; } Construct 332
More on Shell Variables 333
Functions 333
Job Control 333
xi Contents

Shell Jobs 333


Stopping Jobs 334
Command Summary 334
The : Command 334
The . Command 334
The alias Command 335
The bg Command 335
The break Command 336
The case Command 336
The cd Command 337
The continue Command 338
The echo Command 338
The eval Command 339
The exec Command 339
The exit Command 340
The export Command 340
The false Command 341
The fc Command 341
The fg Command 342
The for Command 342
The getopts Command 343
The hash Command 344
The if Command 344
The jobs Command 347
The kill Command 347
The newgrp Command 347
The pwd Command 348
The read Command 348
The readonly Command 349
The return Command 349
The set Command 350
The shift Command 352
The test Command 352
The times Command 354
The trap Command 355
The true Command 356
The type Command 356
Contents xv

The umask Command 356


The unalias Command 356
The unset Command 357
The until Command 357
The wait Command 358
The while Command 358

B For More Information 359


Online Documentation 359
Documentation on the Web 360
Books 360
O’Reilly & Associates 360
Pearson 361

Index 363
About the Authors
Stephen Kochan is the author or co-author of several best-selling titles on Unix and the
C language, including Programming in C, Programming in Objective-C, Topics in C
Programming, and Exploring the Unix System. He is a former software consultant for AT&T
Bell Laboratories, where he developed and taught classes on Unix and C programming.

Patrick Wood is the CTO of the New Jersey location of Electronics for Imaging. He was a
member of the technical staff at Bell Laboratories when he met Mr. Kochan in 1985. Together
they founded Pipeline Associates, Inc., a Unix consulting firm, where he was vice president.
They co-authored Exploring the Unix System, Unix System Security, Topics in C Programming,
and Unix Shell Programming.
We Want to Hear from You!
As the reader of this book, you are our most important critic and commentator. We value
your opinion and want to know what we’re doing right, what we could do better, what areas
you’d like to see us publish in, and any other words of wisdom you’re willing to pass our
way.

We welcome your comments. You can email or write directly to let us know what you did or
didn’t like about this book—as well as what we can do to make our books better.

Please note that we cannot help you with technical problems related to the topic of this book, and that
due to the high volume of mail we receive, we might not be able to reply to every message.

When you write, please be sure to include this book’s title and author, as well as your name and phone
or email address.

Email: feedback@developers-library.info

Mail: Reader Feedback


Addison-Wesley Developer’s Library
800 East 96th Street
Indianapolis, IN 46240 USA

Reader Services
Visit our website and register this book at www.informit.com/register for convenient access
to any updates, downloads, or errata that might be available for this book.
This page intentionally left blank
Introduction

It’s no secret that the family of Unix and Unix-like operating systems has emerged over the last
few decades as the most pervasive, most widely used group of operating systems in computing
today. For programmers who have been using Unix for many years, this came as no surprise:
The Unix system provides an elegant and efficient environment for program development.
That’s exactly what Dennis Ritchie and Ken Thompson sought to create when they developed
Unix at Bell Laboratories way back in the late 1960s.

Note
Throughout this book we’ll use the term Unix to refer generically to the broad family
of Unix-based operating systems, including true Unix operating systems such as
Solaris as well as Unix-like operating systems such as Linux and Mac OS X.

One of the strongest features of the Unix system is its wide collection of programs. More than
200 basic commands are distributed with the standard operating system and Linux adds to it,
often shipping with 700–1000 standard commands! These commands (also known as tools)
do everything from counting the number of lines in a file, to sending electronic mail, to
displaying a calendar for any desired year.

But the real strength of the Unix system comes not from its large collection of commands but
from the elegance and ease with which these commands can be combined to perform far more
sophisticated tasks.

The standard user interface to Unix is the command line, which actually turns out to be a
shell, a program that acts as a buffer between the user and the lowest levels of the system itself
(the kernel). The shell is simply a program that reads in the commands you type and converts
them into a form more readily understood by the system. It also includes core programming
constructs that let you make decisions, loop, and store values in variables.

The standard shell distributed with Unix systems derives from AT&T’s distribution, which
evolved from a version originally written by Stephen Bourne at Bell Labs. Since then,
the IEEE has created standards based on the Bourne shell and the other more recent shells.
The current version of this standard, as of this writing, is the Shell and Utilities volume
of IEEE Std 1003.1-2001, also known as the POSIX standard. This shell is what we use as the
basis for the rest of this book.

The examples in this book were tested on a Mac running Mac OS X 10.11, Ubuntu Linux 14.0,
and an old version of SunOS 5.7 running on a Sparcstation Ultra-30. All examples, with the
2 Introduction

exception of some Bash examples in Chapter 14, were run using the Korn shell, although all of
them also work fine with Bash.

Because the shell offers an interpreted programming language, programs can be written,
modified, and debugged quickly and easily. We turn to the shell as our first choice of
programming language and after you become adept at shell programming, you will too.

How This Book Is Organized


This book assumes that you are familiar with the fundamentals of the system and command
line; that is, that you know how to log in; how to create files, edit them, and remove them;
and how to work with directories. In case you haven’t used the Linux or Unix system for a
while, we’ll examine the basics in Chapter 1, “A Quick Review of the Basics.” In addition,
filename substitution, I/O redirection, and pipes are also reviewed in the first chapter.

Chapter 2, “What Is the Shell?,” reveals what the shell really is, how it works, and how it ends
up being your primary method of interacting with the operating system itself. You’ll learn
about what happens every time you log in to the system, how the shell program gets started,
how it parses the command line, and how it executes other programs for you. A key point
made in Chapter 2 is that the shell is just another program; nothing more, nothing less.

Chapter 3, “Tools of the Trade,” provides tutorials on tools useful in writing shell programs.
Covered in this chapter are cut, paste, sed, grep, sort, tr, and uniq. Admittedly, the
selection is subjective, but it does set the stage for programs that we’ll develop throughout the
remainder of the book. Also in Chapter 3 is a detailed discussion of regular expressions, which
are used by many Unix commands, such as sed, grep, and ed.

Chapters 4 through 9 teach you how to put the shell to work for writing programs. You’ll
learn how to write your own commands; use variables; write programs that accept arguments;
make decisions; use the shell’s for, while, and until looping commands; and use the read
command to read data from the terminal or from a file. Chapter 5, “Can I Quote you on
That?”, is devoted entirely to a discussion of one of the most intriguing (and often confusing)
aspects of the shell: the way it interprets quotes.

By that point in the book, all the basic programming constructs in the shell will have been
covered, and you will be able to write shell programs to solve your particular problems.

Chapter 10, “Your Environment,” covers a topic of great importance for a real understanding of
the way the shell operates: the environment. You’ll learn about local and exported variables;
subshells; special shell variables, such as HOME, PATH, and CDPATH; and how to set up
your .profile file.

Chapter 11, “More on Parameters,” and Chapter 12, “Loose Ends,” tie up some loose ends, and
Chapter 13, “Rolo Revisited,” presents a final version of a phone directory program called
rolo that is developed throughout the book.
Accessing the Free Web Edition 3

Chapter 14, “Interactive and Nonstandard Shell Features,” discusses features of the shell that
either are not formally part of the IEEE POSIX standard shell (but are available in most
Unix and Linux shells) or are mainly used interactively instead of in programs.

Appendix A, “Shell Summary,” summarizes the features of the IEEE POSIX standard shell.

Appendix B, “For More Information,” lists references and resources, including the Web sites
where different shells can be downloaded.

The philosophy this book uses is to teach by example. We believe that properly chosen
examples do a far better job of illustrating how a particular feature is used than ten times as
many words. The old “A picture is worth …” adage seems to apply just as well to coding.

We encourage you to type in each example and test it on your own system, for only by doing
can you become adept at shell programming. Don’t be afraid to experiment. Try changing
commands in the program examples to see the effect, or add different options or features to
make the programs more useful or robust.

Accessing the Free Web Edition


Your purchase of this book in any format includes access to the corresponding Web Edition,
which provides several special features to help you learn:

■ The complete text of the book online


■ Interactive quizzes and exercises to test your understanding of the material
■ Updates and corrections as they become available

The Web Edition can be viewed on all types of computers and mobile devices with any modern
web browser that supports HTML5.

To get access to the Web Edition of Shell Programming with Unix, Linux, and OS X all you need to
do is register this book:

1. Go to www.informit.com/register.

2. Sign in or create a new account.

3. Enter ISBN: 9780134496009.

4. Answer the questions as proof of purchase.

The Web Edition will appear under the Digital Purchases tab on your Account page. Click the
Launch link to access the product.
This page intentionally left blank
3
Tools of the Trade

This chapter provides detailed descriptions of some commonly used shell programming tools.
Covered are cut, paste, sed, tr, grep, uniq, and sort. The more proficient you become
at using these tools, the easier it will be to write efficient shell scripts.

Regular Expressions
Before getting into the tools, you need to learn about regular expressions. Regular expressions are
used by many different Unix commands, including ed, sed, awk, grep, and, to a more limited
extent, the vi editor. They provide a convenient and consistent way of specifying patterns to
be matched.

Where this gets confusing is that the shell recognizes a limited form of regular expressions with
filename substitution. Recall that the asterisk (*) specifies zero or more characters to match, the
question mark (?) specifies any single character, and the construct [...] specifies any character
enclosed between the brackets. But that’s not the same thing as the more formal regular expres-
sions we’ll explore. For example, the shell sees ? as a match for any single character, while a
regular expression—commonly abbreviated regex—uses a period (.) for the same purpose.

True regular expressions are far more sophisticated than those recognized by the shell and there
are entire books written about how to assemble really complex regex statements. Don’t worry,
though, you won’t need to become an expert to find great value in regular expressions!

Throughout this section, we assume familiarity with a line-based editor such as ex or ed.
See Appendix B for more information on these editors if you’re not familiar with them, or
check the appropriate man page.

Matching Any Character: The Period (.)


A period in a regular expression matches any single character, no matter what it is. So the
regular expression
r.

matches an r followed by any single character.


52 Chapter 3 Tools of the
Trade

The regular expression


.x.

matches an x that is surrounded by any two characters, not necessarily the same.

We can demonstrate a lot of regular expressions by using the simple ed editor, an old-school
line-oriented editor that has been around as long as Linux have been around.

For example, the ed command


/ ... /

searches forward in the file you are editing for the first line that contains any three characters
surrounded by blanks. But before we demonstrate that, notice in the very beginning of this
example that ed shows the number of characters in the file (248) and that commands like print
(p) can be prefixed with a range specifier, with the most basic being 1,$, which is the first
through last line of the file:
$ ed intro
248
1,$p Print all the lines
The Unix operating system was pioneered by
Ken Thompson and Dennis Ritchie at Bell
Laboratories in the late 1960s. One of the
primary goals in the design of the Unix
system was to create an environment that
promoted efficient program development.

That’s our working file. Now let’s try some regular expressions:
/ ... / Look for three chars surrounded by blanks
The Unix operating system was pioneered by Ken
/ Repeat last search
Thompson and Dennis Ritchie at Bell Laboratories
1,$s/p.o/XXX/g Change all p.os to XXX
1,$p Let’s see what happened
The Unix operating system was XXXneered by
Ken ThomXXXn and Dennis Ritchie at Bell
Laboratories in the late 1960s. One of the
primary goals in the design of the Unix
system was to create an environment that
XXXmoted efficient XXXgram development.

In the first search, ed started searching from the beginning of the file and found that the
sequence “was” in the first line matched the indicated pattern and printed it.

Repeating the search (the ed command /) resulted in the display of the second line of the file
because “and” matched the pattern. The substitute command s that followed specified that all
occurrences of the character p, followed by any single character, followed by the character o
were to be replaced by the characters XXX. The prefix 1,$ indicates that it should be applied
to all lines in the file, and the substitution is specified with the structure s/old/new/g, where s
Regular Expressions 53

indicates it’s a substitution, the slashes delimit the old and new values, and g indicates it should
be applied as many times as needed for each line, not just once per line.

Matching the Beginning of the Line: The Caret (^)


When the caret character ^ is used as the first character in a regular expression, it matches the
beginning of the line. So the regular expression
^George

matches the characters George only if they occur at the beginning of the line. This is actually
known as “left-rooting” in the regex world, for obvious reasons.

Let’s have a look:


$ ed intro
248
/the/
>>in the late 1960s. One of the primary goals in
>>the design of the Unix system was to create an
/^the/ Find the line that starts with the
the design of the Unix system was to create an
1,$s/^/>>/ Insert >> at the beginning of each line
1,$p
>>The Unix operating system was pioneered by Ken
>>Thompson and Dennis Ritchie at Bell Laboratories
>>in the late 1960s. One of the primary goals in
>>the design of the Unix system was to create an
>>environment that promoted efficient program
>>development.

The preceding example also shows how the regular expression ^ can be used to match the
beginning of the line. Here it is used to insert the characters >> at the start of each line.
A command like
1,$s/^/ /

is also commonly used to insert spaces at the start of each line (in this case four spaces would
be inserted).

Matching the End of the Line: The Dollar Sign $


Just as the ^ is used to match the beginning of the line, so the dollar sign $ is used to match
the end of the line. So the regular expression
contents$

matches the characters contents only if they are the last characters on the line. What do you
think would be matched by the regular expression
.$
54 Chapter 3 Tools of the
Trade

Would this match a period character that ends a line? No. Recall that the period matches any
character, so this would match any single character at the end of the line (including a period).

So how do you match a period? In general, if you want to match any of the characters that
have a special meaning in regular expressions, precede the character by a backslash (\) to
override its special meaning. For example, the regular expression
\.$

matches any line that ends in a period, and the regular expression
^\.

matches any line that starts with a period.

Want to specify a backslash as an actual character? Use two backslashes in a row: \\.
$ ed intro
248
/\.$/ Search for a line that ends with a period
development.
1,$s/$/>>/ Add >> to the end of each line
1,$p
The Unix operating system was pioneered by
Ken>> Thompson and Dennis Ritchie at Bell
Laboratories>> in the late 1960s. One of the
primary goals in>> the design of the Unix
system was to create an>> environment that
promoted efficient program>> development.>>
1,$s/..$// Delete the last two characters from each line
1,$p
The Unix operating system was pioneered by
Ken Thompson and Dennis Ritchie at Bell
Laboratories in the late 1960s. One of the
primary goals in the design of the Unix
system was to create an environment that
promoted efficient program development.

A common use of ^ and $ is the regular expression


^$

which matches any line that contains no characters at all. Note that this regular expression is
different from
^ $

which matches any line that consists of a single space character.


Regular Expressions 55

Matching a Character Set: The [...] Construct


Suppose that you are editing a file and want to search for the first occurrence of the characters
the. In ed, this is easy: You simply type the command
/the/

This causes ed to search forward in its buffer until it finds a line containing the indicated
sequence. The first line that matches will be displayed by ed:
$ ed intro
248
/the/ Find line containing the
in the late 1960s. One of the primary goals in

Notice that the first line of the file also contains the word the, except it begins with a capital
T. A regular expression that searches for either the or The can be built using a character set:
the characters [ and ] can be used to specify that one of the enclosed character set is to be
matched. The regular expression
[tT]he

would match a lower- or uppercase t followed immediately by the characters he:


$ ed intro
248
/[tT]he/ Look for the or The
The Unix operating system was pioneered by Ken
/ Continue the search
in the late 1960s. One of the primary goals in
/ Once again
the design of the Unix system was to
create an 1,$s/[aeiouAEIOU]//g Delete all vowels
1,$p
Th nx prtng systm ws pnrd by
Kn Thmpsn nd Dnns Rtch t Bll
Lbrtrs n th lt 1960s. n f th
prmry gls n th dsgn f th nx
systm ws t crt n nvrnmnt tht
prmtd ffcnt prgrm dvlpmnt.

Notice the example in the above of [aeiouAEIOU] which will match a single vowel, either
uppercase or lowercase. That notation can get rather clunky, however, so a range of characters
can be specified inside the brackets instead. This can be done by separating the starting and
ending characters of the range by a dash (-). So, to match any digit character 0 through 9, you
could use the regular expression
[0123456789]

or, more succinctly, you could write


[0-9]
56 Chapter 3 Tools of the
Trade

To match an uppercase letter, use


[A-Z]

To match an upper- or lowercase letter, you write


[A-Za-z]

Here are some examples with ed:


$ ed intro
248
/[0-9]/ Find a line containing a digit
in the late 1960s. One of the primary goals in
/^[A-Z]/ Find a line that starts with an uppercase letter
The Unix operating system was pioneered by Ken
/ Again
Thompson and Dennis Ritchie at Bell Laboratories
1,$s/[A-Z]/*/g Change all uppercase letters to *s
1,$p
*he *nix operating system was pioneered by *en
*hompson and *ennis *itchie at *ell
*aboratories in the late 1960s. *ne of the
primary goals in the design of the *nix
system was to create an environment that
promoted efficient program development.

As you’ll learn below, the asterisk is a special character in regular expressions. However, you
don’t need to put a backslash before it in the replacement string of the substitute command
because the substitution’s replacement string has a different expression language (we did
mention that this can be a bit tricky at times, right?).

In the ed editor, regular expression sequences such as *, ., [...], $, and ^ are only meaningful
in the search string and have no special meaning when they appear in the replacement string.

If a caret (^) appears as the first character after the left bracket, the sense of the match is
inverted. (By comparison, the shell uses the ! for this purpose with character sets.) For example,
the regular expression
[^A-Z]

matches any character except an uppercase letter. Similarly,


[^A-Za-z]

matches any non-alphabetic character. To demonstrate, let’s remove all non-alphabetic


charac- ters from the lines in our test file:
$ ed intro
248
1,$s/[^a-zA-Z]//g Delete all non-alphabetic characters
1,$p
TheUnixoperatingsystemwaspioneeredbyKen
ThompsonandDennisRitchieatBellLaboratories
Regular Expressions 57

InthelatesOneoftheprimarygoalsin
ThedesignoftheUnixsystemwastocreatean
Environmentthatpromotedefficientprogram
development

Matching Zero or More Characters: The Asterisk (*)


The asterisk is used by the shell in filename substitution to match zero or more characters. In
forming regular expressions, the asterisk is used to match zero or more occurrences of the
preceding element of the regular expression (which may itself be another regular expression).

So, for example, the regular expression


X*

matches zero, one, two, three, … capital X’s while the expression
XX*

matches one or more capital X’s, because the expression specifies a single X followed by zero
or more X’s. You can accomplish the same effect with a + instead: it matches one or more of the
preceding expression, so XX* and X+ are identical in function.

A similar type of pattern is frequently used to match one or more blank spaces in a line:
$ ed lotsaspaces
85
1,$p
This is an example of a
file that contains a lot
of blank spaces Change multiple blanks to single blanks
1,$s/ */ /g
1,$p
This is an example of
a file that contains
a lot of blank spaces

The ed command
1,$s/ */ /g

told the program to substitute all occurrences of a space followed by zero or more spaces with a
single space—in other words, to collapse all whitespace into single spaces. If it matches a single
space, there’s no change. But if it matches three spaces, say, they’ll all be replaced by a single
space.

The regular expression


.*

is often used to specify zero or more occurrences of any characters. Bear in mind that a regular
expression matches the longest string of characters that match the pattern. Therefore, used by
itself, this regular expression always matches the entire line of text.
58 Chapter 3 Tools of the
Trade

As another example of the combination of . and *, the regular expression


e.*e

matches all the characters from the first e on a line to the last one.

Note that it doesn’t necessarily match only lines that start and end with an e, however, because it’s not
left- or right-rooted (that is, it doesn’t use ^ or $ in the pattern).
$ ed intro
248
1,$s/e.*e/+++/
1,$p
Th+++n
Thompson and D+++S
in th+++ primary goals
in th+++ an
+++nt program
d+++nt.

Here’s an interesting regular expression. What do you think it matches?


[A-Za-z][A-Za-z]*

This matches any alphabetic character followed by zero or more alphabetic characters. This
is pretty close to a regular expression that matches words and can be used as shown below to
replace all words with the letter X while retaining all spaces and punctuation.
$ ed intro
248
1,$s/[A-Za-z][A-Za-z]*/X/g
1,$p
X X X X X X X
X X X X X X X
X
X X X 1960X. X X X X X
X X X X X X X X X X X
X X X X X
X.

The only thing it didn’t match in this example was the numeric sequence 1960. You can change
the regular expression to also consider a sequence of digits as a word too, of course:
$ ed intro
248
1,$s/[A-Za-z0-9][A-Za-z0-9]*/X/g
l,$p
X X X X X X X
X X X X X X X
X
X X X X. X X X X X
X X X X X X X X X X
X
X X X X X
X.
Regular Expressions 59

We could expand on this to consider hyphenated and contracted words (for example, don’t), but
we’ll leave that as an exercise for you. As a point to note, if you want to match a dash character
inside a bracketed choice of characters, you must put the dash immediately after the left
bracket (but after the inversion character ^ if present) or immediately before the right bracket
for it to be properly understood. That is, either of these expressions
[-0-9]
[0-9-]

matches a single dash or digit character.

In a similar fashion, if you want to match a right bracket character, it must appear after the
opening left bracket (and after the ^ if present). So
[]a-z]

matches a right bracket or a lowercase letter.

Matching a Precise Number of Subpatterns: \{...\}


In the preceding examples, you saw how to use the asterisk to specify that one or more
occurrences of the preceding regular expression are to be matched. For instance, the regular
expression
XX*

means match an X followed by zero or more subsequent occurrences of the letter X. Similarly,
XXX*

means match at least two consecutive X’s.

Once you get to this point, however, it ends up rather clunky, so there is a more general way to
specify a precise number of characters to be matched: by using the construct
\{min,max\}

where min specifies the minimum number of occurrences of the preceding regular expression to
be matched, and max specifies the maximum. Notice that you need to escape the curly brackets
by preceding each with a backslash.

The regular expression


X\{1,10\}

matches from one to 10 consecutive X’s. Whenever there’s a choice, the largest pattern is
matched, so if the input text contains eight consecutive X’s, that is how many will be matched
by the preceding regular expression.

As another example, the regular expression


[A-Za-z]\{4,7\}

matches a sequence of alphabetic letters from four to seven characters long.


60 Chapter 3 Tools of the
Trade

Let’s try a substitution using this notation:


$ ed intro
248
1,$s/[A-Za-z]\{4,7\}/X/g
1,$p
The X Xng X was Xed by
Ken Xn and X X at X XX
in the X 1960s. One of the X X
in the X of the X X was to X
an
XX X Xd Xnt
X XX.

This invocation is a specific instance of a global search and replace in ed (and, therefore, also in
vi): s/old/new/. In this case, we add a range of 1,$ beforehand and the g flag is appended to
ensure that multiple substitutions will occur on each line, as appropriate.

A few special cases of this special construct are worth noting. If only one number is enclosed by
braces, as in
\{10\}

that number specifies that the preceding regular expression must be matched exactly that many
times. So
[a-zA-Z]\{7\}

matches exactly seven alphabetic characters; and


.\{10\}

matches exactly 10 characters no matter what they are:


$ ed intro
248
1,$s/^.\{10\}// Delete the first 10 chars from each line
1,$p
perating system was pioneered by
Ken nd Dennis Ritchie at Bell
Laboratories e 1960s. One of the
primary goals in of the Unix
system was to create an
t that promoted efficient program
t.
1,$s/.\{5\}$// Delete the last 5 chars from each line
1,$p
perating system was pioneered
b nd Dennis Ritchie at Bell
Laborat e 1960s. One of the
primary goa of the Unix
system was to crea
t that promoted efficient
pr t.
Regular Expressions 61

Note that the last line of the file didn’t have five characters when the last substitute command
was executed; therefore, the match failed on that line and thus was left alone because we
specified that exactly five characters were to be deleted.

If a single number is enclosed in the braces, followed immediately by a comma, then at least
that many occurrences of the previous regular expression must be matched, but no upper limit
is set. So
+\{5,\}

matches at least five consecutive plus signs. If more than five occur sequentially in the input
data, the largest number is matched.
$ ed intro
248
1,$s/[a-zA-Z]\{6,\}/X/g Change words at least 6 letters long to X
1,$p
The Unix X X was X by
Ken X and X X at Bell
X
in the late 1960s. One of the X
goals in the X of the Unix X was to
X an
X that X X
X X.

Saving Matched Characters: \(...\)


It is possible to reference the characters matched against a regular expression by enclosing those
characters inside backslashed parentheses. These captured characters are stored in pre-defined
variables in the regular expression parser called registers, which are numbered 1 through 9.

This gets a bit confusing, so take this section slowly!

As a first example, the regular expression


^\(.\)

matches the first character on the line, whatever it is, and stores it into register 1.

To retrieve the characters stored in a particular register, the construct \n is used, where n is a
digit from 1 to 9. So the regular expression
^\(.\)\1

initially matches the first character on the line and stores it in register 1, then matches what-
ever is stored in register 1, as specified by the \1. The net effect of this regular expression is to
match the first two characters on a line if they are both the same character. Tricky, eh?

The regular expression


^\(.\).*\1$

matches all lines in which the first character on the line (^.) is the same as the last character
on the line (\1$). The .* matches all the characters in-between.
62 Chapter 3 Tools of the
Trade

Let’s break this one down. Remember ^ is the beginning of line and $ the end of line. The
simplified pattern is then ..* which is the first character of the line (the first .) followed by
the .* for the rest of the line. Add the \( \) notation to push that first character into register
1 and \1 to then reference the character, and it should make sense to you.

Successive occurrences of the \(...\) construct get assigned to successive registers. So when
the following regular expression is used to match some text
^\(...\)\(...\)

the first three characters on the line will be stored into register 1, and the next three characters
into register 2. If you appended \2\1 to the pattern, you would match a 12-character string
in which characters 1–3 matched characters 10–12, and in which characters 4–6 matched
characters 7–9.

When using the substitute command in ed, a register can also be referenced as part of the
replacement string, which is where this can be really powerful:
$ ed phonebook
114
1,$p
Alice Chebba 973-555-2015
Barbara Swingle 201-555-9257
Liz Stachiw 212-555-2298
Susan 201-555-7776
Goldberg
Tony Iannino 973-555-1295 Switch the two fields
1,$s/\(.*\) \(.*\)/\2 \1/
1,$p
973-555-2015 Alice Chebba
201-555-9257 Barbara Swingle
212-555-2298 Liz Stachiw
201-555-7776 Susan Goldberg
973-555-1295 Tony Iannino

The names and the phone numbers are separated from each other in the phonebook file by a
single tab character. The regular expression
\(.*\) \(.*\)

says to match all the characters up to the first tab (that’s the character sequence .* between the
\( and the \) and assign them to register 1, and to match all the characters that follow the
tab character and assign them to register 2. The replacement string
\2 \1

specifies the contents of register 2, followed by a space, followed by the contents of register 1.

When ed applies the substitute command to the first line of the file:
Alice Chebba 973-555-2015
Regular Expressions 63

it matches everything up to the tab (Alice Chebba) and stores it into register 1, and every-
thing after the tab (973-555-2015) and stores it into register 2. The tab itself is lost because
it’s not surrounded by parentheses in the regex. Then ed substitutes the characters that were
matched (the entire line) with the contents of register 2 (973-555-2015), followed by a space,
followed by the contents of register 1 (Alice Chebba):
973-555-2015 Alice Chebba

As you can see, regular expressions are powerful tools that enable you to match and manipu-
late complex patterns, albeit with a slight tendency to look like a cat ran over your keyboard at
times!

Table 3.1 summarizes the special characters recognized in regular expressions to help you
understand any you encounter and so you can build your own as needed.

Table 3.1 Regular Expression Characters


Notation Meaning Example Matches
. Any character a.. a followed by any two characters
^ Beginning of line ^wood wood only if it appears at the
beginning of the line
$ End of line x$ x only if it is the last character on
the line
^INSERT$ A line containing just the characters
INSERT
^$ A line that contains no characters
* Zero or more x* Zero or more consecutive x’s
64 Chapter 3 Tools of the
Trade
occurrences of xx* One or more consecutive x’s
previous regular Zero or more characters w followed
.*
expression by zero or more characters followed by
w.*s an s
+ One or more x+ One or more consecutive x’s
occurrences of xx+ Two or more consecutive x’s
previous regular One or more characters w followed
.+
expression by one or more characters followed by
w.+s an s
[chars] Any character in [tT] Lower- or uppercase t
chars [a-z] Lowercase letter Lower- or uppercase
[a-zA-Z] letter

[^chars] Any character not [^0-9] Any non-numeric character Any


in chars [^a-zA-Z] non-alphabetic character
(Continued)
Notation Meaning Example Matches
\{min,max\} At least min and
x\{1,5\} At least 1 and at most 5 x’s
at most max
[0-9]\{3,9\} Anywhere from 3 to 9 successive
occurrences of previ-
digits Exactly 3 digits At least 3 digits
ous regular expres- [0-9]\{3\}
sion [0-9]\{3,\}
\(...\) Save characters
^\(.\) First character on the line; stores it
matched between
^\(.\)\1 in register 1
parentheses in next
register (1-9) ^\(.\)\(.\) First and second characters on the
line if they’re the same
First and second characters on the
line; stores first character in register
1 and second character in register 2

cut
This section teaches you about a useful command known as cut. This command comes in
handy when you need to extract (that is, “cut out”) various fields of data from a data file or the
output of a command. The general format of the cut command is
cut -cchars file

where chars specifies which characters (by position) you want to extract from each line of
file. This can consist of a single number, as in -c5 to extract the fifth character from each line
of input; a comma-separated list of numbers, as in -c1,13,50 to extract characters 1, 13, and
50; or a dash-separated range of numbers, as in -c20-50 to extract characters 20 through 50,
inclusive. To extract characters to the end of the line, you can omit the second number of the
range so
cut -c5- data

extracts characters 5 through the end of the line from each line of data and writes the results
to standard output.

If file is not specified, cut reads its input from standard input, meaning that you can use cut
as a filter in a pipeline.

Let’s take another look at the output from the who command:
$ who
root console Feb 24 08:54
steve tty02 Feb 24 12:55
george tty08 Feb 24 09:15
dawn tty10 Feb 24 15:55
$
cut 65

As shown, four people are logged in. Suppose that you just want to know the names of the
logged-in users and don’t care about what terminals they are on or when they logged in. You
can use the cut command to cut out just the usernames from the who command’s output:
$ who | cut –c1-8 Extract the first 8 characters
root
steve
george
dawn
$

The –c1-8 option to cut specifies that characters 1 through 8 are to be extracted from each
line of input and written to standard output.

The following shows how you can tack a sort to the end of the preceding pipeline to get a
sorted list of the logged-in users:
$ who | cut –c1-8 | sort
dawn
george
root
steve
$

Note, this is our first three-command pipe. Once you get the concept of output connected to
subsequent input, pipes of three, four or more commands are logical and easy to assemble.

If you wanted to see which terminals were currently being used or which pseudo or virtual
terminals were in use, you could cut out just the tty field from the who command output:
$ who | cut –c10-16
console
tty02
tty08
tty10
$

How did you know that who displays the terminal identification in character positions
10 through 16? Simple! You executed the who command at your terminal and counted out
the appropriate character positions.

You can use cut to extract as many different characters from a line as you want. Here, cut is
used to display just the username and login time of all logged-in users:
$ who | cut –c1-8,18-
root Feb 24 08:54
steve Feb 24 12:55
george Feb 24 09:15
dawn Feb 24 15:55
$
66 Chapter 3 Tools of the
Trade

The option -c1-8,18- specifies “extract characters 1 through 8 (the username) and also
characters 18 through the end of the line (the login time).”

The -d and -f Options


The cut command with its -c flag is useful when you need to extract data from a file or
command, provided that file or command has a fixed format.

For example, you could use cut with the who command because you know that the
usernames are always displayed in character positions 1–8, the terminal in 10–16, and the
login time in 18–29. Unfortunately, not all your data will be so well organized!

For instance, take a look at the /etc/passwd file:


$ cat /etc/passwd
root:*:O:O:The Super User:/:/usr/bin/ksh
cron:*:1:1:Cron Daemon for periodic
tasks:/:
bin:*:3:3:The owner of system files:/:
uucp:*:5:5::/usr/spool/uucp:/usr/lib/uucp/uucico
asg:*:6:6:The Owner of Assignable
Devices:/:
steve:*.:203:100::/users/steve:/usr/bin/ksh
other:*:4:4:Needed by secure program:/:
$

/etc/passwd is the master file that contains the usernames of all users on your computer
system. It also contains other information such as user ID, home directory, and the name of the
program to start up when that particular user logs in.

Quite clearly, the data in this file does not line up anywhere near as neatly as the who’s output
does. Therefore extracting a list of all the users of your system from this file cannot be done
using the -c option to cut.

Upon closer inspection of the file, however, it’s clear that fields are separated by a colon charac-
ter. Although each field may not be the same length from one line to the next, you can “count
colons” to get the same field from each line.

The -d and -f options are used with cut when you have data that is delimited by a particular
character, with -d specifying the field seperator delimiter and -f the field or fields you want
extracted. The invocation of the cut command becomes
cut -ddchar –ffields file

where dchar is the character that delimits each field of the data, and fields specifies the
fields to be extracted from file. Field numbers start at 1, and the same type of formats can be
used to specify field numbers as was used to specify character positions before (for example,
-fl,2,8, -fl-3, -f4-).

To extract the names of all users from /etc/passwd, you could type the following:
$ cut -d: -f1 /etc/passwd Extract field 1
root
cron
bin
cut 67

uucp
asg
steve
other
$

Given that the home directory of each user is in field 6, you can match up each user of the
system with their home directory:
$ cut -d: -f1,6 /etc/passwd Extract fields 1 and 6
root:/
cron:/
bin:/
uucp:/usr/spool/uucp
asg:/
steve:/users/steve
other:/
$

If the cut command is used to extract fields from a file and the -d option is not supplied, cut
uses the tab character as the default field delimiter.

The following depicts a common pitfall when using the cut command. Suppose that you
have a file called phonebook that has the following contents:
$ cat phonebook
Alice Chebba 973-555-2015
Barbara Swingle 201-555-9257
Jeff Goldberg 201-555-3378
Liz Stachiw 212-555-2298
Susan Goldberg 201-555-7776
Tony Iannino 973-555-1295
$

If you just want to get the names of the people in your phone book, your first impulse would
be to use cut as shown:
$ cut -c1-15 phonebook
Alice Chebba 97
Barbara Swingle
Jeff Goldberg 2
Liz Stachiw 212
Susan Goldberg
Tony Iannino 97
$

Not quite what you want! This happened because the name is separated from the phone
number by a tab character, not a set of spaces. As far as cut is concerned, tabs count as a
single character when using the -c option. Therefore cut extracts the first 15 characters
from each line, producing the results shown.
68 Chapter 3 Tools of the
Trade

In a situation where the fields are separated by tabs, you should use the -f option to
cut instead:
$ cut -f1
phonebook Alice
Chebba Barbara
Swingle Jeff
Goldberg
Liz Stachiw
Susan
Goldberg Tony
Iannino
$

Recall that you don’t have to specify the delimiter character with the -d option because
cut defaults to a tab character delimiter.

How do you know in advance whether fields are delimited by blanks or tabs? One way to find
out is by trial and error, as shown previously. Another way is to type the command
sed -n l file

at your terminal. If a tab character separates the fields, \t will be displayed instead of the tab:
$ sed -n l phonebook
Alice Chebba\t973-555-2015
Barbara Swingle\t201-555-9257
Jeff Goldberg\t201-555-3378
Liz Stachiw\t212-555-2298
Susan Goldber\t201-555-7776
Tony Iannino\t973-555-1295
$

The output verifies that each name is separated from each phone number by a tab character.
The stream editor sed is covered in more detail a bit later in this chapter.

paste
The paste command is the inverse of cut: Instead of breaking lines apart, it puts them
together. The general format of the paste command is
paste files

where corresponding lines from each of the specified files are “pasted” or merged together
to form single lines that are then written to standard output. The dash character - can also be
used in the files sequence to specify that input is from standard input.

Suppose that you have a list of names in a file called names:


$ cat
names Tony
Emanuel
Lucy
paste 69

Ralph
Fred
$

Suppose that you also have a second file called numbers that contains corresponding phone
numbers for each name in names:
$ cat numbers
(307) 555-5356
(212) 555-3456
(212) 555-9959
(212) 555-7741
(212) 555-0040
$

You can use paste to print the names and numbers side-by-side as shown:
$ paste names numbers Paste them together
Tony (307) 555-5356
Emanuel (212) 555-3456
Lucy (212) 555-9959
Ralph (212) 555-7741
Fred (212) 555-0040
$

Each line from names is displayed with the corresponding line from numbers, separated by
a tab.

The next example illustrates what happens when more than two files are specified:
$ cat addresses
55-23 Vine Street, Miami
39 University Place, New York
17 E. 25th Street, New York
38 Chauncey St., Bensonhurst
17 E. 25th Street, New York
$ paste names addresses numbers
Tony 55-23 Vine Street, Miami (307) 555-5356
Emanuel 39 University Place, New (212) 555-3456
York
Lucy 17 E. 25th Street, New York (212) 555-9959
Ralph 38 Chauncey St., (212) 555-7741
Bensonhurst
Fred 17 E. 25th Street, New York (212) 555-0040
$

The -d Option
If you don’t want the output fields separated by tab characters, you can specify the -d option
to specify the output delimiter:
-dchars
70 Chapter 3 Tools of the
Trade

where chars is one or more characters that will be used to separate the lines pasted together.
That is, the first character listed in chars will be used to separate lines from the first file that
are pasted with lines from the second file; the second character listed in chars will be used to
separate lines from the second file from lines from the third, and so on.

If there are more files than there are characters listed in chars, paste “wraps around” the list
of characters and starts again at the beginning.

In the simplest form of the -d option, specifying just a single delimiter character causes that
character to be used to separate all pasted fields:
$ paste -d'+' names addresses numbers
Tony+55-23 Vine Street, Miami+(307) 555-5356
Emanuel+39 University Place, New York+(212) 555-
3456 Lucy+17 E. 25th Street, New York+(212) 555-
9959 Ralph+38 Chauncey St., Bensonhurst+(212) 555-
7741 Fred+17 E. 25th Street, New York+(212) 555-
0040

Notice that it’s always safest to enclose the delimiter characters in single quotes. The reason
why will be explained shortly.

The -s Option
The -s option tells paste to paste together lines from the same file, not from alternate files. If
just one file is specified, the effect is to merge all the lines from the file together, separated by
tabs, or by the delimiter characters specified with the -d option.
$ paste -s names Paste all lines from names
Tony Emanuel Lucy Ralph Fred
$ ls | paste -d' ' -s - Paste ls’s output, use space as delimiter
addresses intro lotsaspaces names numbers phonebook
$

In the former example, the output from ls is piped to paste which merges the lines
(-s option) from standard input (-), separating each field with a space (-d' ' option). You’ll
recall from Chapter 1 that the command
echo *

would have also listed all the files in the current directory, perhaps slightly less complicated
than ls | paste.

sed
sed is a program used for editing data in a pipe or command sequence. It stands for stream
editor. Unlike ed, sed cannot be used interactively, though its commands are similar. The
general form of the sed command is
sed command file
sed 71

where command is an ed-style command applied to each line of the specified file. If no file is
specified, standard input is assumed.

As sed applies the indicated command or commands to each line of the input, it writes
the results to standard output.

Let’s have a look. First, the intro file again:


$ cat intro
The Unix operating system was pioneered by
Ken Thompson and Dennis Ritchie at Bell
Laboratories in the late 1960s. One of the
primary goals in the design of the Unix
system was to create an environment that
promoted efficient program development.
$

Suppose that you want to change all occurrences of “Unix” in the text to “UNIX.” This can be
easily done in sed as follows:
$ sed 's/Unix/UNIX/' intro Substitute Unix with UNIX
The UNIX operating system was pioneered by
Ken Thompson and Dennis Ritchie at Bell
Laboratories in the late 1960s. One of the
primary goals in the design of the UNIX
system was to create an environment that
promoted efficient program development.
$

Get into the habit of enclosing your sed command in single quotes. Later, you’ll know when
the quotes are necessary and when it’s better to use double quotes instead.

The sed command s/Unix/UNIX/ is applied to every line of intro. Whether or not the line
is modified, it gets written to standard output. Since it’s in the data stream also note that
sed makes no changes to the original input file.

To make the changes permanent, you must redirect the output from sed into a temporary file
and then replace the original file with the newly created one:
$ sed 's/Unix/UNIX/' intro > temp Make the changes
$ mv temp intro And now make them permanent
$

Always make sure that the correct changes were made to the file before you overwrite the
original; a cat of temp would have been smart before the mv command overwrote the original
data file.

If your text included more than one occurrence of “Unix” on a line, the above sed would have
changed just the first occurrence to “UNIX.” By appending the global option g to the end of the
substitute command s, you can ensure that multiple occurrences on a line will be changed.
72 Chapter 3 Tools of the
Trade

In this case, the sed command would read


$ sed 's/Unix/UNIX/g' intro > temp

Now suppose that you wanted to extract just the usernames from the output of who. You
already know how to do that with the cut command:
$ who | cut -cl-8
root
ruth
steve
pat
$

Alternatively, you can use sed to delete all the characters from the first space (which marks
the end of the username) through the end of the line by using a regular expression:
$ who | sed 's/ .*$//'
root
ruth
steve
pat
$

The sed command substitutes a blank space followed by any characters up through the end of
the line ( .*$) with nothing (//); that is, it deletes the characters from the first blank to the end
of the line for each input line.

The -n Option
By default, sed writes each line of input to standard output, whether or not it gets changed.
Sometimes, however, you’ll want to use sed just to extract specific lines from a file. That’s what
the -n flag is for: it tells sed that you don’t want it to print any lines by default. Paired with
that, use the p command to print whichever lines match your specified range or pattern. For
example, to print just the first two lines from a file:
$ sed -n '1,2p' intro Just print the first 2 lines
The UNIX operating system was pioneered by Ken
Thompson and Dennis Ritchie at Bell
Laboratories
$

If, instead of line numbers, you precede the p command with a sequence of characters enclosed in
slashes, sed prints just the lines from standard input that match that pattern. The following
example shows how sed can be used to display just the lines that contain a particular string:
$ sed -n '/UNIX/p' intro Just print lines containing UNIX
The UNIX operating system was pioneered
by Ken the design of the UNIX system was
to create an
$
sed 73

Deleting Lines
To delete lines of text, use the d command. By specifying a line number or range of numbers,
you can delete specific lines from the input. In the following example, sed is used to delete the
first two lines of text from intro:
$ sed '1,2d' intro Delete lines 1 and 2
in the late 1960s. One of the primary
goals in the design of the UNIX system was
to create an environment that promoted
efficient program development.
$

Remembering that by default sed writes all lines of the input to standard output, the remain-
ing lines in text—that is, lines 3 through the end—simply get written to standard output.

By preceding the d command with a pattern, you can used sed to delete all lines that contain
that text. In the following example, sed is used to delete all lines of text containing the
word UNIX:
$ sed '/UNIX/d' intro Delete all lines containing UNIX
Thompson and Dennis Ritchie at Bell
Laboratories in the late 1960s. One of the
primary goals in environment that promoted
efficient program development.
$

The power and flexibility of sed goes far beyond what we’ve shown here. sed has facilities
that enable you to loop, build text in a buffer, and combine many commands into a single editing
script. Table 3.2 shows some more examples of sed commands.

Table 3.2 sed Examples


sed Command Description
sed '5d' Delete line 5
sed '/[Tt]est/d' Delete all lines containing Test or test
sed -n '20,25p' text Print only lines 20 through 25 from text
sed '1,10s/unix/UNIX/g' intro Change unix to UNIX wherever it appears in the
first 10 lines of intro
sed '/jan/s/-1/-5/' Change the first -1 to -5 in all lines containing jan
sed 's/...//' data Delete the first three characters from each line of
data
sed 's/...$//' data Delete the last 3 characters from each line of data
sed -n 'l' text Print all lines from text, showing non-printing
characters as \nn (where nn is the octal value of
the character), and tab characters as \t
74 Chapter 3 Tools of the
Trade

tr
The tr filter is used to translate characters from standard input. The general form of the
command is
tr from-chars to-chars

where from-chars and to-chars are one or more characters or a set of characters. Any
character in from-chars encountered on the input will be translated into the corresponding
character in to-chars. The result of the translation is written to standard output.

In its simplest form, tr can be used to translate one character into another. Recall the file
intro from earlier in this chapter:
$ cat intro
The UNIX operating system was pioneered by
Ken Thompson and Dennis Ritchie at Bell
Laboratories in the late 1960s. One of the
primary goals in the design of the UNIX
system was to create an environment that
promoted efficient program development.
$

The following shows how tr can be used to translate all letter e’s to x’s:
$ tr e x < intro
Thx UNIX opxrating systxm was pionxxrxd by
Kxn Thompson and Dxnnis Ritchix at Bxll
Laboratorixs in thx latx 1960s. Onx of thx
primary goals in thx dxsign of thx UNIX
systxm was to crxatx an xnvironmxnt that
promotxd xfficixnt program dxvxlopmxnt.
$

The input to tr must be redirected from the file intro because tr always expects its input to
come from standard input. The results of the translation are written to standard output, leaving
the original file untouched. Showing a more practical example, recall the pipeline that you
used to extract the usernames and home directories of everyone on the system:
$ cut -d: -f1,6 /etc/passwd
root:/
cron:/
bin:/
uucp:/usr/spool/uucp
asg:/
steve:/users/steve
other:/
$

You can translate the colons into tab characters to produce a more readable output simply by
tacking an appropriate tr command to the end of the pipeline:
tr 75

$ cut -d: -f1,6 /etc/passwd | tr : ' '


root /
cron /
bin /
uucp /usr/spool/uucp
asg /
steve /users/steve
other /
$

Enclosed between the single quotes is a tab character (even though you can’t see it—just take
our word for it). It must be enclosed in quotes to keep it from being parsed and discarded by
the shell as extraneous whitespace.

Working with characters that aren’t printable? The octal representation of a character can be
given to tr in the format
\nnn

where nnn is the octal value of the character. This isn’t used too often, but can be handy
to remember.

For example, the octal value of the tab character is 11, so another way to accomplish the
colon-to-tab transformation is to use the tr command
tr : '\11'

Table 3.3 lists characters that you’ll often want to specify in octal format.

Table 3.3 Octal Values of Some ASCII Characters


Character Octal value
Bell 7
Backspace 10
Tab 11
Newline 12
Linefeed 12
Formfeed 14
Carriage Return 15
Escape 33

In the following example, tr takes the output from date and translates all spaces into newline
characters. The net result is that each field of output appears on a different line:
$ date | tr ' ' '\12' Translate spaces to newlines
Sun
76 Chapter 3 Tools of the
Trade

Jul
28
19:13:46
EDT
2002
$

tr can also translate ranges of characters. For example, the following shows how to translate all
lowercase letters in intro to their uppercase equivalents:
$ tr '[a-z]' '[A-Z]' < intro
THE UNIX OPERATING SYSTEM WAS PIONEERED BY
KEN THOMPSON AND DENNIS RITCHIE AT BELL
LABORATORIES IN THE LATE 1960S. ONE OF THE
PRIMARY GOALS IN THE DESIGN OF THE UNIX
SYSTEM WAS TO CREATE AN ENVIRONMENT THAT
PROMOTED EFFICIENT PROGRAM DEVELOPMENT.
$

The character ranges [a-z] and [A-Z] are enclosed in quotes to keep the shell from
interpreting the pattern. Try the command without the quotes and you’ll quickly see that the
result isn’t quite what you seek.

By reversing the two arguments to tr, you can use the command to translate all uppercase
letters to lowercase:
$ tr '[A-Z]' '[a-z]' < intro
the unix operating system was pioneered by
ken thompson and dennis ritchie at bell
laboratories in the late 1960s. one of the
primary goals in the design of the unix
system was to create an environment that
promoted efficient program development.
$

For a more interesting example, try to guess what this tr invocation accomplishes:
tr '[a-zA-Z]' '[A-Za-z]'

Figured it out? This turns uppercase letters into lowercase, and lowercase letters into uppercase.

The -s Option
You can use the -s option to “squeeze” out multiple consecutive occurrences of characters in
to-chars. In other words, if more than one consecutive occurrence of a character specified in
to-chars occurs after the translation is made, the characters will be replaced by a single
character.

For example, the following command translates all colons into tab characters, replacing
multiple tabs with single tabs:
tr -s ':' '\11'
tr 77

So one colon or several consecutive colons on the input will be replaced by a single tab
character on the output.

Note that '\t' can work in many instances instead of '\11', so be sure to try that if you want
things to be a bit more readable!

Suppose that you have a file called lotsaspaces that has contents as shown:
$ cat lotsaspaces
This is an example of a
file that contains a lot
of blank spaces.
$

You can use tr to squeeze out the multiple spaces by using the -s option and by specifying a
single space character as the first and second argument:
$ tr –s ' ' ' ' < lotsaspaces
This is an example of
a file that contains
a lot of blank
spaces.
$

This tr command in effect says, “translate occurrences of space with another space, replacing
multiple spaces in the output with a single space.”

The –d Option
tr can also be used to delete individual characters from the input stream. The format of tr in
this case is
tr -d from-chars

where any character listed in from-chars will be deleted from standard input. In the following
example, tr is used to delete all spaces from the file intro:
$ tr -d ' ' < intro
TheUNIXoperatingSystemwaspioneeredbyKen
ThompsonandDennisRitchieatBellLaboratories
inthelate1960s.0neoftheprimarygoalsin
thedesignoftheUNIXSystemwastocreatean
environmentthatpromotedefficientprogram
development.
$

You probably realize that you could have also used sed to achieve the same results:
$ sed 's/ //g' intro
TheUNIXoperatingsystemwaspioneeredbyKen
ThompsonandDennisRitchieatBellLaboratories
inthelate1960s.0neoftheprimarygoalsin
thedesignoftheUNIXsystemwastocreatean
environmentthatpromotedefficientprogram
78 Chapter 3 Tools of the
Trade

development.
$

This is not atypical for the Unix system; there’s almost always more than one approach to
solving a particular problem. In the case we just saw, either approach is satisfactory (that is, tr
or sed), but tr is probably a better choice because it is a much smaller program and likely to
execute faster.

Table 3.4 summarizes how to use tr for translating and deleting characters. Bear in mind that
tr works only on single characters. So if you need to translate anything longer than a single
character (say all occurrences of unix to UNIX), you have to use a different program, such as
sed, instead.

Table 3.4 tr Examples


tr Command Description
tr 'X' 'x' Translate all capital X’s to small x’s.
tr '()' '{}' Translate all open parentheses to open braces, all closed
parentheses to closed braces
tr '[a-z]' '[A-Z]' Translate all lowercase letters to uppercase
tr '[A-Z]' '[N-ZA-M]' Translate uppercase letters A–M to N–Z, and N–Z to A–M,
respectively
tr ' ' ' ' Translate all tabs (character in first pair of quotes) to spaces
tr -s ' ' ' ' Translate multiple spaces to single spaces
tr -d '\14' Delete all formfeed (octal 14) characters
tr -d '[0-9]' Delete all digits

grep
grep allows you to search one or more files for a pattern you specify. The general format of this
command is
grep pattern files

Every line of each file that contains pattern is displayed at the terminal. If more than one file
is specified to grep, each line is also preceded by the name of the file, thus enabling you to
identify the particular file that the pattern was found in.

Let’s say that you want to find every occurrence of the word shell in the file ed.cmd:
$ grep shell ed.cmd
files, and is independent of the
shell. to the shell, just type in a
q.
$

This output indicates that two lines in the file ed.cmd contain the word shell.
grep 79

If the pattern does not exist in the specified file(s), the grep command simply displays nothing:
$ grep cracker ed.cmd
$

You saw in the section on sed how you could print all lines containing the string UNIX
from the file intro with the command
sed -n '/UNIX/p' intro

But you could also use the following grep command to achieve the same result:
grep UNIX intro

Recall the phonebook file from before:


$ cat phonebook
Alice Chebba 973-555-2015
Barbara Swingle 201-555-9257
Jeff Goldberg 201-555-3378
Liz Stachiw 212-555-2298
Susan Goldberg 201-555-7776
Tony Iannino 973-555-1295
$

When you need to look up a particular phone number, the grep command comes in handy:
$ grep Susan phonebook
Susan Goldberg 201-555-7776
$

The grep command is particularly useful when you have a lot of files and you want to find
out which ones contain certain words or phrases. The following example shows how the grep
command can be used to search for the word shell in all files in the current directory:
$ grep shell *
cmdfiles:shell that enables sophisticated
ed.cmd:files, and is independent of the
shell. ed.cmd:to the shell, just type in a
q. grep.cmd:occurrence of the word shell:
grep.cmd:$ grep shell *
grep.cmd:every use of the word
shell.
$

As noted, when more than one file is specified to grep, each output line is preceded by the
name of the file containing that line.

As with expressions for sed and patterns for tr, it’s a good idea to enclose your grep pattern
inside a pair of single quotes to “protect” it from the shell. Here’s an example of what can
happen if you don’t: say you want to find all the lines containing asterisks inside the file
stars; typing
grep * stars
80 Chapter 3 Tools of the
Trade

doesn’t work as you’d hope because the shell sees the asterisk and automatically substitutes the
names of all the files in your current directory!
$ ls
circles
polka.dots
squares
stars
stripes
$ grep * stars
$

In this case, the shell took the asterisk and substituted the list of files in your current directory.
Then it started execution of grep, which took the first argument (circles) and tried to find it in
the files specified by the remaining arguments, as shown in Figure 3.1.

circles
polka.dots
arguments squares
grep
stars
stripes
stars

Figure 3.1 grep * stars

Enclosing the asterisk in quotes, however, blocks it from being parsed and interpreted by
the shell:
$ grep '*' stars
The asterisk (*) is a special character that
***********
5 * 4 = 20
$

The quotes told the shell to leave the enclosed characters alone. It then started execution of
grep, passing it the two arguments * (without the surrounding quotes; the shell removes them
in the process) and stars (see Figure 3.2).

*
grep
arguments stars

Figure 3.2 grep '*' stars


grep 81

There are characters other than * that have a special meaning to the shell and must be quoted
when used in a pattern. The whole topic of how quotes are handled by the shell is admittedly
tricky; an entire chapter—Chapter 5—is devoted to it.

grep takes its input from standard input if no filename is specified. So you can use grep as
part of a pipe to scan through the output of a command for lines that match a specific pattern.
Suppose that you want to find out whether the user jim is logged in. You can use grep to
search through who’s output:
$ who | grep jim
jim tty16 Feb 20 10:25
$

Note that by not specifying a file to search, grep automatically scans standard input.
Naturally, if the user jim were not logged in, you would get a new command prompt without
any preceding output:
$ who | grep jim
$

Regular Expressions and grep


Let’s take another look at the intro file:
$ cat intro
The UNIX operating system was pioneered by
Ken Thompson and Dennis Ritchie at Bell
Laboratories in the late 1960s. One of the
primary goals in the design of the UNIX
system was to create an environment that
promoted efficient program development.
$

grep allows you to specify your pattern using regular expressions as in ed. Given
this information, it means that you can specify the pattern
[tT]he

to have grep search for either a lower- or uppercase T followed by the characters he.

Here’s how to use grep to list all the lines containing the characters the or The:

$ grep '[tT]he' intro


The UNIX operating system was pioneered by
Ken in the late 1960s. One of the primary
goals in the design of the UNIX system was
to create an
$

A smarter alternative might be to utilize the -i option to grep which makes patterns case
insensitive. That is, the command
grep –i 'the' intro
82 Chapter 3 Tools of the
Trade

tells grep to ignore the difference between upper and lowercase when matching the pattern
against the lines in intro. Therefore, lines containing the or The will be printed, as will lines
containing THE, THe, tHE, and so on.

Table 3.5 shows other types of regular expressions that you can specify to grep and the types of
patterns they’ll match.

Table 3.5 Some qrep Examples


Command Prints
grep '[A-Z]' list Lines from list containing a capital letter
grep '[0-9]' data Lines from data containing a digit
grep '[A-Z]...[0-9]' list Lines from list containing five-character patterns
that start with a capital letter and end with a digit
grep '\.pic$' filelist Lines from filelist that end with .pic

The -v Option
Sometimes you’re interested not in finding the lines that contain a specified pattern, but those
that don’t. That’s what the -v option is for with grep: to reverse the logic of the matching
task.
In the next example, grep is used to find all the lines in intro that don’t contain the
pattern UNIX.
$ grep -v 'UNIX' intro Print all lines that don't contain UNIX
Thompson and Dennis Ritchie at Bell
Laboratories in the late 19605. One of the
primary goals in environment that promoted
efficient program development.
$

The -l Option
At times, you may not want to see the actual lines that match a pattern but just seek the names
of the files that contain the pattern. For example, suppose that you have a set of C programs
in your current directory (by convention, these filenames end with the filename suffix .c), and
you want to know which use a variable called Move_history. Here’s one way of finding
the answer:
$ grep 'Move_history' *.c Find Move_history in all C source files
exec.c:MOVE Move_history[200] = {0};
exec.c: cpymove(&Move_history[Number_half_moves -1],
exec.c: undo_move(&Move_history[Number_half_moves-1],;
exec.c: cpymove(&last_move,&Move_history[Number_half_moves-1]);
exec.c: convert_move(&Move_history[Number_half_moves-1]),
exec.c: convert_move(&Move_history[i-1]),
grep 83

exec.c: convert_move(&Move_history[Number_half_moves-1]),
makemove.c:IMPORT MOVE Move_history[];
makemove.c: if ( Move_history[j].from != BOOK (i,j,from)
OR makemove.c: Move_history[j] .to != BOOK (i,j,to)
) testch.c:GLOBAL MOVE Move_history[100] = {0};
testch.c: Move_history[Number_half_moves-1].from = move.from;
testch.c: Move_history[Number_half_moves-1].to = move.to;
$

Sifting through the preceding output, you discover that three files—exec.c, makemove.c, and
testch.c—use the variable.

Add the -l option to grep and you instead get a list of files that contain the specified pattern,
not the matching lines from the files:
$ grep -l 'Move_history' *.c List the files that contain Move_history
exec.c
makemove.c
testch.c
$

Because grep conveniently lists the files one per line, you can pipe the output from grep -l
into wc to count the number of files that contain a particular pattern:
$ grep -l 'Move_history' *.c | wc -l
3
$

The preceding command shows that precisely three C program files reference the variable
Move_history. Now, just to make sure you’re paying attention, what are you counting if you
use grep without the -l option and pipe the output to wc -l?

The -n Option
If the -n option is used with grep, each line from the file that matches the specified pattern
is preceded by its corresponding line number. From previous examples, you saw that the file
testch.c was one of the three files that referenced the variable Move_history; the
following shows how you can pinpoint the precise lines in the file that reference the variable:
$ grep -n 'Move_history' testch.c Precede matches with line numbers
13:GLOBAL MOVE Move_history[100] = {0};
197: Move_history[Number_half_moves-1].from = move.from;
198: Move_history[Number_half_moves-1].to = move.to;
$

As you can see, Move_history is used on lines 13, 197, and 198 in testch.c.

For Unix experts, grep is one of the most commonly used programs because of its
flexibility and sophistication with pattern matching. It’s one well worth studying.
84 Chapter 3 Tools of the
Trade

sort
At its most basic, the sort command is really easy to understand: give it lines of input and
it’ll sort them alphabetically, with the result appearing as its output:
$ sort
names
Charlie
Emanuel
Fred
Lucy
Ralph
Tony
Tony
$

By default, sort takes each line of the specified input file and sorts it into ascending order.

Special characters are sorted according to the internal encoding of the characters. For example,
the space character is represented internally as the number 32, and the double quote as the
number 34. This means that the former would be sorted before the latter. Particularly for
other languages and locales the sorting order can vary, so although you are generally assured
that sort will perform as expected on alphanumeric input, the ordering of foreign language
characters, punctuation, and other special characters is not always what you might expect.

sort has many options that provide more flexibility in performing your sort. We’ll just
describe a few of the options here.

The -u Option
The -u option tells sort to eliminate duplicate lines from the output.
$ sort -u names
Charlie
Emanuel
Fred
Lucy
Ralph
Tony
$

Here you see that the duplicate line that contained Tony was eliminated from the output.
A lot of old-school Unix people accomplish the same thing by using the separate program
uniq, so if you read system shell scripts you’ll often see sequences like sort | uniq.
Those can be replaced with sort -u!
sort 85

The -r Option
Use the -r option to reverse the order of the sort:
$ sort -r names Reverse sort
Tony
Tony
Ralph
Lucy
Fred
Emanuel
Charlie
$

The -o Option
By default, sort writes the sorted data to standard output. To have it go into a file, you can use
output redirection:
$ sort names > sorted_names
$

Alternatively, you can use the -o option to specify the output file. Simply list the name of the
output file right after the -o:
$ sort names -o sorted_names
$

This sorts names and writes the results to sorted_names.

What’s the value of the –o option? Frequently, you want to sort the lines in a file and have the
sorted data replace the original. But typing
$ sort names > names
$

won’t work—it ends up wiping out the names file! However, with the -o option, it is okay
to specify the same name for the output file as the input file:
$ sort names -o names
$ cat
names
Charlie
Emanuel
Fred
Lucy
Ralph
Tony
Tony
$
86 Chapter 3 Tools of the
Trade

Tip
Be careful if your filter or process is going to replace your original input file and make sure that
it’s all working as you expect prior to having the data overwritten. Unix is good at a lot of things,
but there’s no unremove command to recover lost data or lost files.

The -n Option
Suppose that you have a file containing pairs of (x, y) data points as shown:
$ cat data
5 27
2 12
3 33
23 2
-5 11
15 6
14 -9
$

And suppose that you want to feed this data into a plotting program called plotdata, but that
the program requires that the incoming data pairs be sorted in increasing value of x (the first
value on each line).

The -n option to sort specifies that the first field on the line is to be considered a
number, and the data is to be sorted arithmetically. Compare the output of sort used
without
the -n option and then with it:
$ data
sort
-5 11
14 -9
15 6
2 12
23 2
3 33
5 27 Sort arithmetically
$ -n data
sort
-5 11
2 12
3 33
5 27
14 -9
15 6
23 2
sort 87

Skipping Fields
If you had to sort your data file by the y value—that is, the second number in each line—you
could tell sort to start with the second field by using the option
-k2n

instead of -n. The -k2 says to skip the first field and start the sort analysis with the second
field of each line. Similarly, -k5n would mean to start with the fifth field on each line and then
sort the data numerically.
$ -k2n data Start with the second field in the sort
sort
14 -9
23 2
15 6
-5 11
2 12
5 27
3 33
$

Fields are delimited by space or tab characters by default. If a different delimiter is to be used,
the -t option must be used.

The -t Option
As mentioned, if you skip over fields, sort assumes that the fields are delimited by space or tab
characters. The -t option can indicate otherwise. In this case, the character that follows the -t
is taken as the delimiter character.

Consider the sample password file again:


$ cat /etc/passwd
root:*:0:0:The super User:/:/usr/bin/ksh
steve:*:203:100::/users/steve:/usr/bin/ksh
bin:*:3:3:The owner of system files:/:
cron:*:l:l:Cron Daemon for periodic tasks:/:
george:*:75:75::/users/george:/usr/lib/rsh
pat:*:300:300::/users/pat:/usr/bin/ksh
uucp:nc823ciSiLiZM:5:5::/usr/spool/uucppublic:/usr/lib/uucp/uucico
asg:*:6:6:The Owner of Assignable Devices:/:
sysinfo:*:10:10:Access to System Information:/:/usr/bin/sh
mail:*:301:301::/usr/mail:
$

If you wanted to sort this file by username (the first field on each line), you could just issue the
command
sort /etc/passwd
88 Chapter 3 Tools of the
Trade

To sort the file instead by the third colon-delimited field (which contains what is known
as your user ID), you would want an arithmetic sort, starting with the third field (-k3),
and specifying the colon character as the field delimiter (-t:):
$ sort -k3n -t: /etc/passwd Sort by user id
root:*:0:0:The Super User:/:/usr/bin/ksh
cron:*:l:l:Cron Daemon for periodic tasks:/:
bin:*:3:3:The owner of system files:/:
uucp:*:5:5::/usr/spool/uucppublic:/usr/lib/uucp/uucico
asg:*:6:6:The Owner of Assignable Devices:/:
sysinfo:*:10:10:Access to System Information:/:/usr/bin/sh
george:*:75:75::/users/george:/usr/lib/rsh
steve:*:203:100::/users/steve:/usr/bin/ksh
pat:*:300:300::/users/pat:/usr/bin/ksh
mail:*:301:301::/usr/mail: .
$

Here we’ve bolded the third field of each line so that you can easily verify that the file was
sorted correctly by user ID.

Other Options
Other options to sort enable you to skip characters within a field, specify the field to end the
sort on, merge sorted input files, and sort in “dictionary order” (only letters, numbers, and
spaces are used for the comparison). For more details on these options, look under sort in your
Unix User’s Manual.

uniq
The uniq command is useful when you need to find or remove duplicate lines in a file.
The basic format of the command is
uniq in_file out_file

In this format, uniq copies in_file to out_file, removing any duplicate lines in the process.
uniq’s definition of duplicated lines is consecutive lines that match exactly.

If out_file is not specified, the results will be written to standard output. If in_file is also
not specified, uniq acts as a filter and reads its input from standard input.

Here are some examples to see how uniq works. Suppose that you have a file called names with
contents as shown:
$ cat
names
Charlie
Tony
Emanuel
Lucy
uniq 89

Ralph
Fred
Tony
$

You can see that the name Tony appears twice in the file. You can use uniq to remove such
duplicate entries:
$ uniq names Print unique lines
Charlie
Tony
Emanuel
Lucy
Ralph
Fred
Tony
$

Oops! Tony still appears twice in the preceding output because the multiple occurrences are not
consecutive in the file, and thus uniq’s definition of duplicate is not satisfied. To remedy this
situation, sort is often used to get the duplicate lines adjacent to each other, as mentioned
earlier in the chapter. The result of the sort is then run through uniq:
$ sort names | uniq
Charlie
Emanuel
Fred
Lucy
Ralph
Tony
$

The sort moves the two Tony lines together, and then uniq filters out the duplicate line (but
recall that sort with the -u option performs precisely this function).

The -d Option
Frequently, you’ll be interested in finding just the duplicate entries in a file. The -d option
to uniq can be used for such purposes: It tells uniq to write only the duplicated lines to
out_file (or standard output). Such lines are written just once, no matter how many
consecutive occurrences there are.
$ sort names | uniq -d List duplicate lines
Tony
$

As a more practical example, let’s return to our /etc/passwd file. This file contains
information about each user on the system. It’s conceivable that over the course of adding and
removing users from this file that perhaps the same username has been inadvertently entered
90 Chapter 3 Tools of the
Trade

more than once. You can easily find such duplicate entries by first sorting /etc/passwd and
piping the results into uniq -d as done previously:
$ sort /etc/passwd | uniq -d Find duplicate entries in /etc/passwd
$

There are no duplicate full line /etc/passwd entries. But you really want to find duplicate
entries for the username field, so you only want to look at the first field from each line (recall
that the leading characters of each line of /etc/passwd up to the colon are the username).
This can’t be done directly through an option to uniq, but can be accomplished by using cut
to extract the username from each line of the password file before sending it to uniq.
$ sort /etc/passwd | cut -f1 -d: | uniq -d Find duplicates
cem
harry
$

It turns out that there are multiple entries in /etc/passwd for cem and harry. If you
wanted more information on the particular entries, you could now grep them from
/etc/passwd:
$ grep -n 'cem' /etc/passwd
20:cem:*:91:91::/users/cem:
166:cem:*:91:91::/users/cem:
$ grep -n 'harry' /etc/passwd
29:harry:*:103:103:Harry Johnson:/users/harry:
79:harry:*:90:90:Harry Johnson:/users/harry:
$

The -n option was used to find out where the duplicate entries occur. In the case of cem, there
are two entries on lines 20 and 166; in harry’s case, the two entries are on lines 29 and 79.

Other Options
The -c option to uniq adds an occurrence count, which can be tremendously useful in scripts:
$ sort names | uniq –c Count line occurrences
1 Charlie
1 Emanuel
1 Fred
1 Lucy
1 Ralph
2 Tony
$

One common use of uniq -c is to figure out the most common words in a data file,
easily done with a command like:
tr '[A-Z]' '[a-z]' datafile | sort | uniq -c | head
uniq 91

Two other options that we don’t have space to describe more fully let you tell uniq to ignore
leading characters/fields on a line. For more information, consult the man page for your
particular implementation of uniq with the command man uniq.

We would be remiss if we neglected to mention the programs awk and perl, which can
be useful when writing shell programs too. They are both big, complicated programming
environments unto themselves, however, so we’re going to encourage you to check out Awk
—A Pattern Scanning and Processing Language, by Aho, et al., in the Unix Programmer’s
Manual, Volume II for a description of awk, and Learning Perl and Programming Perl, both from
O’Reilly and Associates, offering a good tutorial and reference on the language, respectively.
This page intentionally left blank
Index

Symbols
& (ampersand)
&& construct, 161–162
background execution of loops, 177
command sequences and, 322
* (asterisk)
with case statement, 155
filename substitution, 24–25, 47, 331
pattern matching, 57–59, 63, 242, 336
` (back quote), 114–115
\ (backslash)
escaping characters with, 111–112
inside double quotes, 112–114
line continuation, 112
overview, 322
[ ] (brackets), 139–140
^ (caret), 53, 63
: (colon)
: (null) command, 160–161, 334
in directories, 218
((.)) construct, 311
(.) construct, 47, 231–234, 332
\(.\) construct, 61–63, 64
\{.\} construct, 59–61, 64
#! construct, 289–290
|| construct, 161–162
$(.) construct, 115–118
[!] construct, 242
364 [.] construct
" (double 203
[.] a
quotes)
constru %o format
m
ct, 55– backslash (\) specification
57, e in, 112–114 character, 203
242, t grouping %s format
336 character specification
e
{ .; } sequences character, 203
r
constru with, 109– %u format
ct, 231– 111 specification
234, s character, 203
332 - (hyphen)
u command %X format
>& specification
constru b options, 8
character, 203
ct, 261– s job control,
262 315
t
>&- printf format
i
constru specificatio
ct, 262 t n modifier,
<&- u 206
constru t < (left
ct, 262 arrow), 48–
i
$ 49, 331–332
(dollar o
!
sign) n (logic
c $ al
o {paramete negat
r}, 239– ion)
m 240 opera
m $ tor,
a {paramete 143,
r:+ 322
n
value},
d 242 $(( ))
operator,
$ 103
p {paramete
r:= % (percent
r sign)
value},
o 241 %% format
m $ specification
{paramete character,
p 203
r:-value},
t 240 %b format
, $ specification
{paramete character,
203
4
r:?value},
241–242 %c format
3 specification
pattern
matching, character,
53–54, 63 203
p
variable %d format
a
substitution, specification
r 98–100 character,
%x n a ation
format t t modifie
specificati s r, 206
on f
p ? (question mark)
character, e
203 filename substitution,
c c
25–27, 47, 331
job i
control, o pattern matching, 242,
f
315 m 336
i
p m c >>
a a a (redirect
t ion
t n
i append)
t d o characte
e , n rs, 31–
r 32, 48–
m 49
n 2
o <<
0 d
m i (r
2
a f e
. (period)
i d
t dot (.) e
c command, 227– ir
r
230, 334–335
h , e
pattern 2
i matching, 51– c
0
n 53, 63 ti
6
g | (pipe) o
symbol # (pound sign)
, comments, 96 n
|| construct,
161–162 pattern a
2 matching, 243 p
case command,
4 159–160 pr p
2 loops, 178–179 i
e
n
– pipeline
t n
process, 33–34,
2 49, 321, 322 f d
4 f
+ (plus sign) )
o
3
job control, 315 r c
pattern m h
w matching, 63 a
a
i pr t
s r
t i
n p a
h e
t c
f c
i t
p f
o f e
r i
r r
i m c
s ,

3
s 2 2
h 6 1
e 2
l –
'
l 2
6
4 (
a
> (right arrow), s
r
30–32, 48–49, i
c 331–332
n
h ;
g
i
l
v
( e
e
s
e
c q
m
r u
i
e o
c
a t
o
t e
l
i s
o
o )
n
n ,
)
,
,
1
2 0
6 3
5
4 6

– ,
1
2
0
6 1 8
7 7 ~ (tilde)
9 substitution,
318–319, 329
s –
_ (underscore),
y 1 322
n 8 $? $! variable,
t 0 323
a , $! variable,
x 257–258, 323
bases, numbers in different bases 365
background d
$- variable, 16–17
323 execution of e
accessing
$# command loops, 177 f
variable, history. command i
122–123, See
323 sequences n
command
$$ history and, 322 i
variable, add archives, t
198–199, program, creating, i
323 125–127, 264–267
277 o
$* a
variable, addi n
r
123–124, program,
166, 323 200 g
o
$@ ali s
f
variable,
as ,
166–167,
323 co p
$0 m r 3
variable,
ma o 2
245, 323
nd, g 1
${n}
variable, 30 r
128 7– a p
${variable} 30 m a
construct,
98–100, 9, , s
102–103 33 s
5 i
1
ali n
2
as g
A es
2
$# variable,
– 122–123
defining,
-a 307–309 1 $* variable,
123–124
(logi removing, 2
309 ${n} special
cal 3
allexport variable,
AND shell mode, 128
) 350 a phonebook
oper alternative r file
ator, format for
g example,
test
143– u 124–128
command,
144 139–140 m
acce ampersand e
ss (&)
n
mod &&
construct, t
es, 161–162 s
positional (*) format
parameters, a with case statement, specific
121–122 155
r ation
shift filename
command, i charact
substitution, 24–25,
128–129 t 47, 331 er, 203
processing, 343– h pattern matching, back
344 57–59, 63, 242, 336
m quote
arithmetic asy
e (`),
arithmetic nc
t 114–
expansion, 103–
104 i hro
115
arithmetic c no
background
expressions, 330 us
integer types, execution
e 304–305 job commands, 36–37
x number s, jobs, 316–317
p s in
25 loops, 177
r differen
7 backslash (\)
t bases,
aut es
c 305– c
om
o 306 a
ati
m overvie p
call i
m w, 303–
y n
a 304 g
loa
n line sorting, 86 c
de h
d arrays, 309–314
d a
, AS r
fun
CII a
cti c
1 ch
on t
1 ar e
9 s,
act r
– 30 s
ers
1 3 w
, it
2 aw
oct h
0 k ,
al
co 1
val
m 1
i
ue 1
n ma –
s
t nd, 1
of,
e 91 1
75 2
g
ast i
e B
eri n
r
%b
sk si
d o
e n
,
d
o 1
u 1
b 2
l overview, 322
e ba
s
e
q
s,
u n
o u
t m
b
e
e
s r
, s
i
n
1
d
1 i
2 f
– e
r
1
e
1 n
4 t
b
a
l
s
i e
n s,
e 3
0
5
c –
o 3
n 0
6
t
i
n
u
a
t
i
366 Bash shell
g
Bash shell. ell
See also e ]
Lab
nonstand ,
orat )
ard shell
1 ,
features orie
6 1
compatibil s, 1
ity body of 3
b
summary, loops, 164
319–320 g 9
books,
history of, c recommende –
289 d, 360–361 1
o
W B 4
m
eb o 0
m
do u
a break
cu r
n comm
m n
d and,
en e
, 174–
tat
, 176,
3
io
1 336
n,
6 S breaki
36
– t ng out
0
3 e of
b
1 p loops,
e
7 h 174–
gi
, e 176
n
ni 3 n

n 3 ,

g 5
b
C
of 1
li l
C compiler, 360
n o
b %c format
e, c
r specificatio
m k
a n
at s
c character,
c o
k 203 caret
hi f
e (^), 53, 63
n s
t case command
g, t
s debugging, 157–
5 o
159
3 r overview, 336–
a ( 337
B
[
pattern- cha ,
characters. See also
matching ngin text
characters, g 3 ASCII
155–156 d 4 chara
pipe i 7 cters,
symbol (|), r – octal
159–160 e 3 value
syntax, c 4 s of,
153–154
t 8 75
cat phonebook
o chara
command, file entries,
7 r 281–283 cter
cd i character seque
command, e sets, nces
12–15, matching,
317–318, s do
55–57
337 , ub
cdh le
function,
1 qu
312–314
2 ot
CDPATH
variable, – es
323, 337 1 (")
cdtest 5 ,
program,
225 , 10

c 9–

h
3 11
3 1
a
7 sin
n
gle
g
g qu
e
r ot
pr
o es
o
u ('),
gr
p 10
a
5–
m
i 10
,
d 8
2
cutting, 64–68
8 ( deleti
1– G ng
2 I from
8 D input
3 ) strea
m i e 7
, l g
e i e
7 n n n
7 a n d
– m i
7 e n o
8 s g f
allowed
characters, 6
e o l
special
c f i
characters, 28
h n
format
o specifica l e
i tion i ,
n characte n
rs
g e 5
(printf),
, 202– , 3
205 –
6 pattern matching 5 5
e a 3 4
s n filena
c y c me
a h subst
p c a itutio
i h r n,
n a a 25–
g r c 27
, a t matc
c e hed
1 t r chara
1 e cters,
1 r s savin
– , e g,
1 t 61–
1 5 s 63
2 1 , overv
– iew,
i 5 5 155–
n 3 5 156
– paramete
f b 5 r
sub , l ut,
stit e 74–
77
utio 1
child processes, 257
n, 1 q
cla
242 1 u
use
– – o
s,
244 1 t
els
pre 1 e
cise 4 s e,

nu 145

mb d ( –

er o ' 147
of u ) ,
cha b , 345
ract l –
ers, e 1 346
59– 0 clos
61 q 5 ing
zer u – sta
o or o 1 nda
mor t 0 rd
e e 8 out
cha s tra put,
ract n
262
s
ers, ( col
l
57– " a on
59 ) ti
(:)
, n
quote characters
g :
b f
a 1 r (
c 0 o
n
m
k 9
u
s – s l
l 1 t
l
a 1 a
n )
s 1
d
h a c
s r
o
d
( i
i m
n n m
) g p a
n
d
,

3
3
4

i
n

d
i
r
e
c
t
o
r
i
e
s
,

2
1
8
commands 367
vi line (.)
for 9
comma edit construct
3 mode,
nd , 115–
– 294–
$* 118 back
296,
varia 9
ble, 326– quote (`),
6 329
166 114–115
$@ con definition
variab trol
c of, 112
le,
166– o ling expr command,
167 size 119–120
m
overv of, command-line
m
iew, editing
a 292
163– command history
edit
n
166, accessin
ing
d g with
342– co vi,
343 mm 294–
witho h and 296
ut in i s controlling size
eleme s of, 292
in,
nt, t emacs line edit mode
341
167– o command
comm
history,
168 r and accessing,
comm y line. 296–298
and
accessing See overview, 296–
cycle,
emacs 298
43 comm
line edit overview, 291
c ands
o mode, Comm
m 294–296
and
m fc
not
a comman
found
n d, 301,
error,
d 326
94
history
comman comm
f d, 294– and
296,
i promp
300–
l 301 t, 43
e quoting, comm
329 and
s
r substi
, comman
d, 301– tution
303 $
vi line 123–124 –
edit
} $ 3
mode
{ 4
c
o c n 4
m o }
m n
a p
n s s h
d t p o
r e n
h
u c
i e
s c i b
t t a o
o , l o
r
y k
, 3 v
3 a f
a
2 r
c i
c i l
e a a e
s l b
s
i l
i e
n a e x
g s , a
, ,
m
2 1 p
9 3 2 l
4 0 8 e

7
2 ,
9 – o 124–128
6 3 v positional
overv 0 e parameters, 121–
iew, 122
9 r
292– shift command,
294 , v 128–129
comman i awk, 91
ds 3 e bg, 316–317, 335
(.) 3 w break, 174–176, 336
construc
5
t, 332 , case
{ arguments,
passing debugging, 157–159
3 overview, 336–337
$# variable,
. 122–123 4 p
; $* variable, 3 a
t | c
t ) l e
e
, e d
r
n , i
- t
1
m 4 i
a 5
t 9
3 n
c – g
h c
1
i
6
o c
n
g 0
m o
m m
c a m
h s
n a
a y
d n
r n
a d
t
c h s
t a
i
e x
r s i
,
s t n
, o ,
1 r
1 5 y 3
5
3 a 4
5
– – c 1
1 1 c command re-entry, 326
5
5 e command summary,
6 37–38
4 s
p
cat, 7 continue, 176–177, 338
s
i
cd, 12–15, cp, 8, 18–19
i
p 317–318, 337
n
e c
g
o
,
s m
y m
3
m a
2
b n
6
o d

l
3
c
2
y
( 9
368 commands
82–83
cut 255–257, jobs, 315, 347
339 -n option,
-d kill, 315, 347
83
optio exec, 230–
ln, 20–23
n, 66– 231, 262, 339 overview,
68 78–81 ls, 7, 15–17
exit, 147–
-f 148, 340 regular man, 359
optio export, 340– expressio mkdir, 17–18
n, 341 ns, 81–82
66– multiple
expr, 119– -v option,
68 82 commands
120
overv grouping, on same
false, 341
iew, 231–234 line, 36
64– fc, 301, 326–
66 Z01.2304, hash, 344 mv, 8–9,
341 his 19
date,
5, fg, 316–317, tor newgrp, 347–348
95– 342
96, y, null (:), 160–161, 334
for
237 29 od, 251
$*
dot (.), 4– o
variable,
334–
166 29 p
335
$@ 6, t
echo
variable, 30
esc 166–167 i
ape 0– o
overview,
cha 30
163–166, n
ract
ers, 1 s
342–343
187 if. ,
– without
See
188 in
if 8
ove element,
rvie sta
167–168
w, te p
6, format of,
338 321–322 me a
– get nt s
339
opt info, 359 t
emacs
s, e
line
180 -d option, 69–70
edit
– overview, 68–69
comma
184 -s option, 70
nds,
, p
299–
343 e
300
– r
enterin
344 l
g, 43–
gre ,
44
p
eval,
-l option, 9
1 0 g m (rolo), 193–
5 199
p f i mycp program, 185–
o 193
r n
r overview, 348–349
i m f
syntax, 185
n a o
t readonly, 349
t r
f m
s
exampl a
p
e, 206–
e t
207
c i
f i
o o
f
r i n
m c
a a a
t t
b
i
s o o
p n u
e
t
c m
i ,
o
f d
i i 3
c f 5
a i
t 6
e
i r
o s p
n , s
,
c 2
h 0
a 5 3
r – 7
a 2
c pwd, 12, 95–96,
0 348
t 6
e quoting, 329
r syntax, 202
r, 301–303
s p
read
, r
exit status,
i 199–202
2
n menu-
0
t driven
2
phone
– i
progra
2 n
compatibility of shells 369
-x option, tr
readyonl ckgro
y, 254
246 uc
und,
shift, 128– t,
return,
36– 129, 352
271, 349
1
37 set ski
ret 1
-- option, ppi
urn
248–250 5–
ng
ing
IFS 1
in
info variable, 1
251–254 loo
rm 8
mo ps,
atio ba
nito 176
n ck
r –
abo q
opti 177
ut, u
on, sort
271 ot
331 -k2n
rm,
option, e
9 –
87 (`)
rmdir, 332
-n ,
22–23 wit option,
86 1
scanning h
twice -o 1
no
before option, 4–
executi arg 85
1
ng, um other
1
255– ents options,
257 88 5
,
sed overview, de
247
d 84 fi
ove
com -r option, ni
mand rvie 85
, 73 ti
w, -t option,
exam 87–88 o
239
ples, -u n
73 ,
option, of,
-n 321 84 1
optio , substitution
n, 72 1
350 $
synta 2
– (
x, 70– expr
72 351 . command,
se positiona ) 119–120
l
nd
parame
in ters, c
g reassign o
to ing,
n
247–
ba s
248
test, 135, 352– -s option, 76– p i
354 77 i q
tilde trap n
-c option, 90
substitution, g
318–319, 329 e -
x d
times, 354, o
e
355–356 n
c o
tr u
o p
-d option, t t
n
77–78 i i
e
examples, o o
78 n n
l ,
o i
w
c n
i 8
t e 9
t
a , –
h
l 9
1 0
n
v 7 o
o
a 9 v
l – e
a
u 1 r
r
e 8 v
g
s 0 i
u e
m umask, 356
w
o e u ,
f n n
t 8
a
A s 8
, l –
S
C i 8
2 9
I a
I 5 u
9 s n
– , s
c 2 e
h 6 t
a 3 ,
0 2
r 0
ignored signals, 5
a
260 9 4
c ,
t overview, 258– ,
2
e 259 7
r signal numbers, 1
3
s 258 ,
5 3
, trap reset, 261
6 5
true, 356 7
7 u
type, 271
5 u n
t t
overview, n
y i
74–76
l who, 5–6
s
,
c ,
1 o 319–320
7
m
0
– m
1
e
7
4 n
,
t
3 s
5 ,
7
v
i 9
6
l ,
i
n
e 3
2
e
2
d
i
t c
o
c
o m
m p
m a
a
t
n
d i
s b
, i

2
l
9 i
6 t
,
y
3
2 o
6 f

3
2 s
9 h
wait, 257, 358 e
wc, 7, 95–96 l
while, 168–170 l
370 conditional statements
t 142–143
conditiona 147
l i integer
exit
statement n operators,
command
s. See also 140–142
, 147–148 g
loops logical AND
exit status, ,
&& operator (-a),
131–135
construct
, 161–162 nu 143–144
1
| ll logical
4
| co negation
8
m operator (!),

c m 143 logical
1
o an OR operator
4
n d (-o), 144
9
s (:), overview, 135
t 16 string
t
r 0– operators,
e
u 16 135–139
s
c 1 contents
t
t pi of files,
i
, pe displaying
n
sy ,7
g
1 m continuati
6 bo on of
c
1 l lines, 112
o
– (|) continue
n
1 , command,
d
6 15 176–177,
i
2 9–
t 338
16
i Coordinat
i 0
o ed
f sy
n Universal
case nt
comm s Time, 237
ax,
and, copying
153– 13
i files
160 1
n to
elif testing
constr condition alternativ another
uct, s in, 131– e format directory,
148– 144
151 for test, 18–19
n
else 139–140 mycp
constr e
file program
uct, s
operators, echo
145–
esc 188– i
creating
ap 190 l
aliases, 307–309
e i e directories, 17–18
ch n s functions, 268
ara t , p
cte o
o
rs,
7 i
18 n n
7– e t
18 c
w e
8 p
r
fin
f s
al
i c
co
l o t
de
e m o
list
, m
ing
a v
,
n a
19 8
d r
0–
, i
19 c a
3 o b
ini 8
u l
tial ,
n e
co
t s
de
i 1 ,
list
n 8
ing
g – 2
,
1 5
18
9 7
5– w
18 o
s
7 r
h
rev d
e
ise s
l
d
l
co
i
de
n a
list
r
ing
c
, f
h
i 2 links cha
v 6 , 23 rac
e data. See also I/O ter
s (input/output) s,
c redirection
, 66–
u extracting
68
t c
2 fields, 66–68
u
6 printing
t
4 c command
– o information, 356
c date/time, 5, 95–96
2 m
o f
6 m
m o
7 a
m r
ctype program, n
155–156, 158– a m
d
159 n
-d option, 66–68 a
c d t
-f option, 66–68
u t
overview, 64–66
r s e
Cygwin, 360
r y d
e n o
D t
n u
t d command, 73 a t
-d file operator, x p
142–143
, u
d %d
i t,
form
6 2
r at 4 0
e spec – 2
c ifica 6 –
t tion 6 2
o char
0
r acte d 7
y r, e li
, 203 l s
dan i t
2 glin m o
2 g i f
5 sym t a
boli e c

c r ti
2
v l
e o
c
)
e o
j x m
a m
o
m
b p a
s l n
, e d
,
1
3 9 s
3
4 y

7 1 n
to working 9 t
directory, 9 a
348 m x
reading y ,
exit status, c
199–202
p
m 1
e 8
n p 5
u
- r data formatting
d o (rolo program),
r 273–274
g
i
r
v
e a
n m
,
p
h
o 1
n 8
e
5

p
r 1
o 9
g 3
r
a
m r
e
(
a
r
o d
duplicate entries, finding 371
command, copying
date th -x,
66–68
comma files to,
157– paste
nd, 5, 18–19
95–96, 159 command,
237 69–70 creating,
defini
date/ti sort 17–18
ng. command,
me, current directory,
printing See 87–88 225–226
, 5, 95– creati developmen directory files, 6
96 t of Unix, 1
ng home, 10–12, 217
d /
defini moving
b d
tions files
e
(func between,
p v
tion) 19
r /
creating, pathnames
o 268 t
, 10–12
g removing, t
removing, 22–23
271
r y
structure of, 9–10
deleting
a ,
aliases, 309
m
characters
, 1
from input
2 7
stream, 77–
2 78 8
7 directories,
– 22–23 d
2 duplicate i
2 lines
r
9 sort
comman e
d, 84 c
d uniq t
comman
e d, 88–89 o
b files, 9 r
u function i
g definitions, e
271
g s
lines, 73
i cha
phonebook
n file entries, ngi
g 127–128, ng,
280–281 12–
delimiter 15,
w characters
337
i cut
working m p m
directory
o l m
definiti
d a e
on of,
10 e y n
displayi , i t
ng, 12 s
n
p ,
g
2
r
error messages,
4
i 245 9
6 file contents, 7
n 6
t ph
i d on h
n i eb e
g s oo r
p k e
l fil
t
a e d
o
y en o
,
tri c
es, u
p
3 27 m
r
4 8– e
o 27
8 n
g 9 t
r va s
d
a ria s
i
m bl h
s e
, e
a l
val l
b
2 ue
l a
7 s,
i r
98
8 c
n
– h

g 10 i
2
0 v
7 e
t working
9 directory, 12
r c
documentation r
a
d books, 360–361 e
c a
i c
e t
s o
i
o m u 7–
n e b 23
,
t s 0
2 e t do
6 r i ub
4 t
– le
s u
2 qu
6 u t
ot
7 b i
es
syntax, s o
262– (")
t n
264 backslash (\) in, 112–
i , 114
online
document t grouping
ation, 359 characte
u 9
Web r
t 8
documenta sequenc
tion, 360 i – es with,
dollar sign o 1 109–
($) n 111
0
c ${parameter}, duplicate entries,
0
o 239–240 finding, 89–90

m ${parameter:
m + value}, 242 d

a $ o
{parameter:=
n t
value}, 241
d $
{parameter:- (
value}, 240
p .
${parameter:?
r )
value}, 241–
o 242
m pattern
matching, 53– c
p
54, 63 o
t
v m
,
a m
r
4 a
i
3 n
a
d
b
p ,
l
a
e
r
2
a
2
s
372 duplicate lines, eliminating
73 147,
duplicate e
lines, -n option, 345–
c
eliminatin 72
346
g h
vi line edit
emacs
sort o mode, 326–
command, 329 line
84 el edit
c
uniq e mode
command, o m
88–89 com
m e
ma
m n
nd
t
a hist
s
ory,
n
acc
E d o
essi
f
escape ng,
characters, a
296
- 187–188 r

e r 298
overview, 6,
338–339 a
o
y
f editing vervi
s
i command- , ew,
line editing
l r 296–
command e
e history, 298
t
292 r ena
o emacs i blin
line edit e g
p
v
mode, trac
e i
296–300 n e
r
overview, g mo
a
291 ,
t de,
3
vi line edit 246
o mode, 0
r 292–296 9 end
– of
, phonebook
file entries, 3
1 line
281–283
1 0 ,
editors
4 elif mat
stream editor
(sed)
construct, chi
2 148–151
– command ng,
syntax, else
1 53–
70–72 clau
4 54
d se,
3 command end
145
, 73 -of-
examples, –
l 4
entries (phonebook e
i 4 file) ,
n pa adding, 125–127,
ss 277
e 2
w displaying, 278–279
1
or editing, 281–283
c 6
ds listing, 283–284
h
at looking up, 124–125,
a 278 s
r lo
removing, 127–128, u
gi 280–281
a b
n, ENV file, 290–291
c s
4 ENV
t h
1 varia
e e
r ble, l
, 290– l
291, s

4 323 (.) construct, 231–


234
5 envir
{
onm
e ent
current directory, .
n
225–226 ;
t
exported variables,
e 211–216
r HOME variable, 217 }
i local variables, 209–
210
n c
PATH variable, 217–
g o
224
c n
.profile file, 235–236
o s
PS1 variable, 216
m t
P
m r
S
a u
2
n c
d t
v
s ,
a
,
r
i 2
4
a 3
3
b 1

l –
2 n g ,
3 d
4 , v 1
a 4
d 2 r 0
o 2 i –
t 7 a 1
– b 4
( 2 l 2
. 3 e

) 0 s
e
o
r
v t
c r
e o
o e
r ,
m x
v
m i
i
a 2 t
e
n 3
w
d 4
, s
, –
h
2
2 3
e
2
1 5 l
2
0 TERM variable, 236– l
7
– 237

2 TZ variable, 236–237
2 m
1 environment
3 control, 49 o
1
0 - d
,
e e

q ,
e
2
x
2
e 3
7 o
c 5
p
e 0
p
c r
a
o a e
s
m t r
s
m o r
i
a r o
n
r ar
s d
Command not err
found, 94 or,
e 35
r esc
r a
o p
e
r
c
h
m ar
e a
s
ct
er
s
s
a (e
g c
e h
o
s
c
, o
m
d m
a
i
n
s d)
p ,
l 1
8
a
7–
y 1
i 8
n 8,
3
g
3
, 8
escaping
2 characters, 111–
112
4
5

s
t
a
n
d
files 373
147–148, zero values, 131
/ e
340 export command,
etc/pas
s
swd, E 340–341
41 ,
X exported
/ 9 variables, 211–
I
etc/pro 216, 332,
4
file, T 340–341
235– e
expr
236 x
s comm
/ e
etc/sha i and,
dow, c
g 119–
41 u
n 120
eval ti
a expres
comma o
l sions
nd, n
, arithmetic
255– background
expressions, 330
257, execution of
regular
339 loops, 177 2
expressions
exclam command 5
[.] construct, 55–
ation execution 8 57
mark comman \(.\) construct,
d files, 61–63
(!), 322 93–96 e
exec \{.\} construct,
scanning x 59–61
comma
comma
nd, i asterisk (*), 57–
230– nd line
t 59
231, twice
262, before
339 executi
ng, s
e
255– t
x 257
a
e in current
t
c shell,
u
u 227–230
s
t function
$? variable,
a execution, 132–135
b 268–269 definition
l program of, 321
execution, non-zero
e
45–47 values, 131
subshell overview,
execution, 131–132
f
332
i read
exit command,
l command, 199–202
caret 8 1 n
(^), 53 1 4 d
d – 2 ,
o 8 –
l 2 1
l 3
overview, 51 4
a 1
period (.), 3
r 51–53 6

summary –
table, 61–63 f 3
s
extracting data a 1
i
cut command l 7
g
delimiter s ,
n characters,
66–68 e

fields, 66–68 3
(
overview, c 4
$
64–66
) o 2
, m
m
f
a
5 F i
n
3 e
d
– - l
,
5 f d
4 s
3 cutting, 66–68
f
g 4 skipping during sort, 87
i
r 1 file descriptors, 261
l
e fc command, f
e 301, 326-
p i
Z01.2304, 341
l
FCEDIT
o e
c variable, 323
p
o f
e
m g o
r
m p
a
a c e
t
n o r
o
d m a
r
, m t
,
a o
r
s X i i
, a n
s b g
h l t
1
e e o
4
l s
2
l , a

, n
1
1 o
4
3 0 t
3
3 1 h
1 – e
f 1 r
i q 0
l u 3 d
e e files i
n s c r
a t o e
m i m c
e o m t
n a o
n r
s
m d y
u
a ,
b
r f
s k i 1
t l 8
i ( e –
t ? s 1
u ) , 9
t ,
i 9 m
o 2 3 y
n 5 – c
asterisk – 9 p
(*), 24–25
2 6
overview,
7 p
47, 331
c r
P
v o o
O
a p g
S
r y r
I
a s
m
, i
n
1 ,
8
5 7

1 d
9 i
3 r
e
i c
n t
t o
o r
y
n
e f
w i
l
f e
i s
l ,
e
, 6

8
c
o
u
n
t
i
n
g

w
o
r
d
374 files
ria
disp at
s ble
layi or
h s,
ng s,
e 10
con 14
l 1–
tent 2–
l 10
s of, 14
, 3
7 3
filenames
dup fil
allowed
licat en 3 characters, 6
e a 3 changing, 8–9
entr m 1 spaces in, 27
ies, e
special
find su characters, 28
q
ing, bs linking, 20–23
u
89– tit listing, 7, 15–17
e
90 ut moving
s
ENV io between
t
, n directories,
i
290 * 19 ordinary
(asterisk), o
– files, 6
24–25 n
291 o
?
executabl v
e files, 94
(que
m
stio er
executi a
n vi
ng in r
mar e
current k
k), w,
shell,
25– 6
227–
( –
27
230 file
? 7
aste
descrip
) p
risk
tors,
, h
(*),
261
24– o
f
25 2 n
i
overview, 5 e
l
47, 331 – b
e
P 2 o
O 7 o
o
S k
p
I adding
e v
X entries to,
r a
125 280–281
s
– .profile, 235– e
236 a 8
127,
r
removing, 9 2
277 c
renaming, 8– h
disp
9 i
layi n s
rolosubs file,
ng g o
264–266
entr w r
ies i t
t
fro h i
m, n
g
278 r g
– e
p
279 l
-
editi l i
ng n
o
entr e
p
ies t s
in, i
o
281 n
i
– ,
n
283
8 t
listi 2
o
ng –
8 ,
entr 3
ies -n option, 83
in, 8
overview, 78–81
283 5
regular
– expressions, 81–
284 82
s
lookin -
p
g up v
e
entrie
s in, c
124– o i
125,
278 p a

removi t l
ng i
entri
o f
es
from, n i
127– , l
128,
e
s n r p
s
, g u
,
t
2
j ,
6 0
o 2
temporary files,
198–199 b –
2 p
filters, 35 s 0
5 r
fi
m i
n t
o n
di o
d t
n ,
i i
g.
f n
S 3
i g
e 4
e
e 2
r
p e
s
s x
at ,
t a
te
o m
rn
p 2 p
m
p 0 l
at
i 5 e
c
n – ,
hi
g 2
n
, 0
g 2
6
fo 0
3
6
re
1 f –
gr 6
o 2
o –
r 0
u 3
m 7
n 1
a format
d 7 speci
t
jo format ficati
specification t on
bs
(printf) e char
b
c acter
d
r h s,
i a 202–
r 205
n a o
c format
g u
t speci
i e t ficati
o 6
n 0

m
o f
d
i u
f n
i c
e
r t
s i
, o
n
2
0 s
5 autom

aticall
2
0 y
6 loaded
printf command functi
syntax, 202
ons,
Fox, Brian, 289
303
Free Software cdh,
Foundation,
289, 360 312–

f 314

s definitions

f creating, 268–
271
.
removing, 271
o
execution, 268–269
r
local variables, 303
g
overview, 333
terminating, 271
w
e
b
s
i
t
e
,

3
if statement 375
m history
-v option, 82
command,
group id m
294–296,
a 300–301.
G (GID),
See also
changing, n
command
347–348 d history
-ge
operator, grouping ,
140–142
command
getopts 3
s, 231–
comma 4
234
nd, 4
groups, 16–
180– 17
184, -gt operator, h
343– 140–142
a
344 s
getty h
progra
H
m. See s
shells i
handing
GID signals with g
(group trap n
command
id),
execution
changin (
with no
g, 347– argume #
348 nts, )
259–
greeting comments,
260 96
s
ignored
pattern
program signals, 260
matching,
, 149– overview, 243
258–259
151, printf format
signal specification
159–160
numbers, modifier, 206
grep 258
here
comman trap reset, documents
261
d shell
h
-l option, archive
82–83 a creation,
-n option, s 264–267
83
h syntax,
overview,
78–81 262–264
regular c HISTFILE
expression o variable, 323
s, 81–82
h 6 debugging, 157– l
home directory, 159 (|
i
10–12, 217 p ),
s
HOME variable, a
t t 1
217, 323
o t 5
H
r e 9
U r
y –
P n
- 1

o m 6
s a 0
f t
i s
c
g h y
U
n i n
n
n ta
a
i g
l x,
x
, c 1
,
h 5
a 3
1 2 r

5 a
c 1
H 8
t 5
I e 4
S r
h elif construct, 148–151
s
T y , else construct, 145–147
S p e
I h 1 x
Z 5
e i
5
E n t

1
v 5 c
( 6
a o
- p
r m
) i
i m
command options, 8 p
a a
job control, 315 e
b n
printf format
l specification d
e modifier, 206 s ,
, y
I m 1

3 if statement b 4

case command o 7
2
– 60 4
1 – 0
4 16 file operators,
142–143
8 1
integer operators,
ov 140–142
e er logical
x vi AND
i ew operator
t , (-a),
34 143–144
s 4– logical
t 34 negation
a 6 operator
t syntax, 131 (!), 143
u testing conditions in
s al
$? variable, t
132–135 e
r
non-zero
values, 131 n
a
overview, 131–
t
132
i
zero values, v
131 e
nesting, 148–149
n f
o
u
r
l m
l a
t
c
o f
m o
m r
a
t
n e
d s
t
,
(
: 1
) 3
9
,

1 1
376 if statement
of, 174–176 on
logi –
info < (left arrow), 331
cal 139
command, exec command,
OR test
359 230–231, 262
ope co
i in-line input
rat mm
n redirection
or and
i shell archive
(- syn
t creation, 264–
o), tax,
267 syntax, 262–
14 135
264
4 IFS p
POSIX shell, 331
ove var r
standard I/O
rvi iab o (input/output), 28–
ew, le, 30
g
13 25 IN
r
5 1– T
a
par 25 si
ent m
4, g
hes ,
es, 32 n
144
3 al
s 4
ign ,
t 0
or 2
r –
ee 5
i 4
of 8
n 3
sh in
g
ell te
mo i g
o n er
de,
p p
35 ar
e u
1 it
r
ign t h
a
ori m
t
ng r et
o
sig e ic
r
nal d expr command,
s 119–120
s, i
, integer types, 304–
26 r 305
0 e numbers in different
1
infinite c bases, 305–306
3
loops, t overview, 303–304
5
breaking out i integer operators,
140– ell
r command, functions
142
fea 301–303 automatic
i
tur vi line edit ally
n
es mode, 294–
296 loaded
t aliases
command-line functions,
e defining, editing 303 local
g 307–309
command variables,
e removing, history, 292
309 303
r e
arrays, 309– integer arithmetic
314
m
integer types, 304–
ac 305
t cd
s numbers
y comm
li in
p and,
n different
e 317–
e bases,
s 318
e 305–306
, comm
di overview,
and
t 303–304
histor
3 m j
y,
0 o o
access
4 d b
ing
– e,
emacs
3 2 c
line edit
0 9 o
mode,
5 6 n
296–298
– t
fc
3 r
i comman
0 o
n d, 301
0 l
t history
command, ov ,
e 300–301 er
r
vi 3
a
e 1
c
w, 5
t 2 –
i 9 3
v 1 1
e vi line edit 7
mode, 292–
296
s o
ENV file, 290–
h 291 r
d tilde ut c
substitution,
e pu t
318–319
r t) i
int
re o
er
o dir n
na
f ec < (left arrow), 331
l
tio exec command, 230–
fie 231, 262
s n
ld in-line
e <&- construct,
se input
a 262
pa redirectio
r >
ra & n, 262–
c
to - 267
h
,
rs, overview,
25 c 331

3 1– o POSIX shell, 331

1 25 n shell

9 4 s archive
int t creation,

s er r 264–267

h pr u standard

e et c I/O

l ed t (input/ou

l pr , tput), 28–

, 30
og
2 l
ra
s 6 o
m
p 2 o
mi
e p
ng
c i s
la
i n ,
ng
f p
ua
y u 1
ge
i t 7
,
n 7
50
g r –
I/
, e 1
O
d 7
(in 8
2 i
pu
9 r
t/o o
0 e
u
t
p
u
t

r
e
d
i
r
e
c
t
i
o
n
exec
comm
and,
230–
231,
262
overvi
ew,
30–32
standa
rd
output
,
closin
g, 262
lines 377
, n
overview, s
48–49, 3 t
y
331–332 4 i
n
in 2 n
programs, c
94 g
h
sta j
r
n o l
o
d b i
a n
s
r o
d c t
u
e o
r s
n o
r j
o t f
o
r, r ,
b
w o
r s
l, 3
i ,
t 3 4
2
i 1 7
n 5
5 referencing,
g 7 333–334

t
sending
o 3
b to
, 1
2 r backgro
7
6 i und,
1 job numbers,
n 37 316–

g k 317
2
6 i i stopped
2 n l jobs,
standard g l 316–
I/O
(input/out t i 317
put), 28– o n stopping, 334
30
g w
ison
f , a
program,
122 o i
r 3 t
e 4 i
g 7 n
J r g
o p
jobs u r f
a n i o
d
r
emacs
L
, K c
-L file operator, o
142–143 m
3
kernel, 1, 39 -le operator, 140– m
5
keyword 142 a
8 n
parameters. le
f d
See
w t
variables - h
a
(shell) kill s i
i s
command, h
t i t
315, 347 o
i f
killing jobs, t r
n
347 i y
g ,
Korn, David, n
289, 360 g
a
f Korn shell. c
o See also p c
r nonstandar o e
d shell s s
features i s
c t
compatibility i
o summary, i n
m 319–320 o g
p history of, n ,
289 a
l l 2
Web
e documentati p 9
t on, 360 a 6
kornshell.co r –
i
m website, a 2
o 360 9
m
n e 8
$! t overview, 296–298
variabl e o
e, 257– r
258 v
s
wait , e
comma r
nd,
257 1 v
2 i
jobs
8
command, e

315, 347
1 w
2 ,
9
line edit modes 2
9 I t
1 a N i
r E n
v c N u
i h O a
c i t
o v i
v
m
e a o
m
a r n
n c ,
i
d r
a
e 1
h b
i a 1
l
s t 2
t e
i
o ,
r o p
y n a
, 3
, s
2
2 t
a 4
c 6 i
c 4 n
e –
l
g
s i
s 2 f
6 n r
i
n 7 e o
g s m
,
s cutting, 64–66

2 y deleting, 73 d
9 n duplicate lines, i
4 eliminating

t f
sort command,
2 a 84 f
9 x e
uniq command,
6 88–89
, r
overview, 292– l
2 e
294
6 i n
in-line input
redirection 2 n t
s – e

h 2 f
e 6 c i
l 4 o l
l L n e
s m i rac
, e n ter
e s,
6 f , 87
8 i –
– l 88
5
6 e du
3
9 , plic
ate
e
o 7 lin
n
u 0 es,
d
t pat eli
p t mi
u e o nat
t r f ing,
n 84
d
l
e m i
l a
n
i t
e
m c
,
i h
t i
n 5
e
g 3
r

s
5
, b
4
e
sorting
6 g
9 arithmetically,
i
86
– n
d
7 n
e
0 i
li
n
m
f g
i
r
t
o
o e
m
f r
c
s
h
a l
a
378 lines
Web 278
t k documentati
f
o i on, 360
listall o
n
o prog r
g
u ram, lo

t 283– o
f
p 284 p
i
u listin s,
l
t g 3
e
files, 7, 15– 4
s 17
f 2
, phonebook
i –
l file entries, 3
e 2 283–284 4
, 0 variables, 3
– 247 lo
8 2 ln command, o
20–23
5 3 p
local
s
variable
o body of, 164
L s, 209–
v breaking out
i 210, 303
e of, 174–176
n logical
r executing in
u AND
v background,
x operator
i 177 for
e (-a),
$* variable, 166
w r 143–144
$@ variable,
, e logical 166–167
s negation overview, 163–
8 o operator (!), 166, 342–343
4 u 143, 322 without in
reve r logical OR element, 167–
rse 168
c operator (-o),
orde
r, 85 e 144 getopts command,
180–184
skip s login cycle,
ped 44 I/O redirection,
books, 360– 177–178
field login shell,
361
s, 87 40–43 piping data into and
online out of, 178–179
l looking up
documentati
i on, 359 phonebook
n overview, entries,
359 124–125,
s 63
k o match
i
p
n M ing
p e patter
MAIL variable,
i 324 ns
n
l MAILCHECK a
g
i variable, 324 n
r n MAILPATH y
e variable, 324
e
m man command,
, c
a 359
i m h
n 1 a
a
i 7 r
n t
9 a
g c
– c
h
c 1 t
o e
8 e
m d
m 0 r
a ,
n u c
d
n h 5
s
t a 1
i i r –
n l a 5
,
, c 3
1 t
7 1 b
e
6
7 r e

1 0 s g
7 – i
,
7 1
s n
terminating 7
a n
, 336
4 i
t v
while, 168–170, n
y 358 i
g
p ls command, 7, n
i 15–17 g
-lt operator, o
n ,
140–142 f
g 6
lu program,
1
124–125, 278 l
o –
i
n
n f savin
e g g,
, l r 61–
i e 63
5 n p over
3 e view,
case , c 51
command,
o paramete
155–156,
336–337 5 m r
character 3 m substituti
sets, 55–57 – a on, 242–
d 5 n 244
u 4 d precise
p f -l option, 82– number
l 83
i of
i -n option, 83
l subpatter
c e
overview, 78– ns, 59–61
a
81
n sum
t
regular
a ma
expressions,
e 81–82 ry
m
of
e -v option, 82 reg
e m ula
n a r
s
t exp
u t
res
r b c sio
i s h n
e e cha
t
ract
s i d
ers,
, t 63–
u c 64
8 t h zero or more characters,
9 57–59
i a
– o r
9 n a
0 , c
t
e 2 e
n 5 r
d – s
2 ,
o 7
nonstandard shell features 379
c 4
math
em o lu program,
2
278
atic d
7
al rem
e
equ 8
pro
l
atio –
gra
n i
2
m,
solv s
7
er 280
t
(ex 9

i
pr), 281
119 n
f
revi
– g
i
120 sed
,
n
menu- cod
1
driven a
e
phone 9
l
program listi
3
c
(rolo) ng,

o
$$ 196
1
variable, d

198–199 9
e
198
add 4
l
program, sam
277 i
ple
l
change s
out
program, i
t
281–283 put,
s
i
data 284
t
formatting n

, 273–274 a
g
287
d l
,
sample runs,
i l
2 195–196
s p
7 tem
p r
4 porary
l o
– files,
a g
2 198–
y r
7 199
a
7 mess
p m
ages
r ,
i (error
o 2
n ),
g 8
i displa
r 3
t ying,
a –
i 245
m 2
a minus
, 8
l
si nds on listi
overview, 27, 47,
gn same ng, 331
(- line, 36 185 P
). mv – O
S command 187 S
e , 8–9, 19 revi I
e my sed X
hy ba code

se listi
ph s
ng,
en na h
188
(-) me e

m pro l
190
kd gra l
ir m, ,
co 24
m 4 3
m my N 3
an cp 1
d, pro -n
17 gra stri
q
– m ng u
18 echo op e
monitor esca era s
option pe tor, t
(set char 13 i
comman acte 7, o
d), 331– rs, 13 n
332 187 8
monitor –
na
m
188
shell me a
final
mode, s r
code
351 filename k
listi substituti
moving
on
ng,
files asterisk
190 (
betwee (*), 24–25
– ?
n
193 )
directori
initi ,
es, 19
al
multiple
code 2
comma
5 i
– f 3 s
2 4 h
7 7 e
s
t – l
v a 3 l
a t 4
r e 8
m
i m n
o
a e e
d
b w
n e
l l
t ,
e i
s
s
, n
, 3
e
5
1 1
1
4 c
0
8 h
1 n
– a
– o
1 r
1 e
4 a
0 x
9 c
3 e
t
filenames c
e
allowed n
characters, 6 r
e
changing, 8–9 , s
w
spaces in, 27 h
g
special e
r 4
characters, 28 l
p 5
pathnames, 10–12 l
-ne operator,
140–142 n
c
n m
o o
e o
m c
s d
m l
t e
a o
i ,
n b
n
d b
g e 3
,
r 5
1 o arrays, 309–314 e
d c s
e d s
n
, i
o
c n
g
o g
l 3
m em
o 5
m acs
b 1
a lin
n e
s n d edi
h o , t
e n mo
l s 3 de,
l t 1 29
a 7 6–
n – 29
m
d 3 8
o
a 1 fc
d
r 8 co
e
d m
,
c ma
o nd,
s
3 m 30
h
5 m 1
e
1 a history command,
l n 300–301
l d r command, 301–
n 303
o vi line edit mode,
f h 294–296
l
e i command-line editing
o
a s command history,
g
t 292
t
o emacs
u
s r line edit
r
h y mode,
e
e , 296–300
s
l overview,
aliases
l a 291
defining, 307–
309 c vi line edit mode,
292–296
removing, 309 c
m
ENV file, 290–291
functions
380 nonstandard shell features
2 ll
autom
9 co
atically o
0 mm
loaded r
t and
functio d
ild (:),
ns, 303 e
e 160
local r
su –
variabl
bst
es, 303 o 161
itu
integer f null
arithmetic tio
val
integer n,
s ues
types, 31
304– e ,
8–
305 a 100
31
numbe r –
9
rs in c 101
no
differen h number
n-
t bases, , program, 154–
ze 155, 201–202
305–
ro nu
306 3
overvie ex mb
1
w, 303– it er 2
9
304 st pro

job at gra
s
control, us m,
315–317 h
e , 253
n
l 13 nu
u
l 1 mb
m
, no ers
b
un in
e
s se differ
r
p t ent
s
e sh bases
,
c ,
ell
i 305–
3 m
f 306
0 od
y job
4 e,
i numb
– 35
n ers,
3 1
g 37
0 n
, signal numbers,
5 u 258, 355
t

o a 1
ct ti 4
o
O al 4
d n

u ,
%o format o
m 3
specificati p
on p 5
e
character, 9
c
203 r
o T
- a
m h
o t
m e
o
a O
r
( p
n
s
l e
d, $(( )), 103
o n
2 arithmetic
g G operators, 330
5
i r file operators,
1
c o 142–143
octal
a u
values
l p
of
ASCII ,
O charact 3
R ers, 75 6
) od 0

comma AND operator,


143–144
o nd,
O
p 251
R
e o

r n
li o
a
n p
t
e e
o
d r
r
o a
,
c t

u o
1 r
m
4 ,
e
4
n
integer operators, cal o (shell)
140–142
ne c o
l
gat i v
o
io a e
g
n t r
i
op e v
c
er s i
a
at , e
l
or 3 w
(!), ,
6
A
14
0
N
3 2

D
log 3
3
ica 9
o 6
l
p 1
OR p
e o
op a
r u
er r
a t
at a
t p
or m
o u
(- e
r t
o), t
output redirection
14 e
( > (right arrow),
4 r
- 30–32
string operators,
a 135–139 exec command,
230–231, 262 s
) test operators, 353–
POSIX shell, u
354
, 331–332 b
options
(command), 8 standard output, s
1 closing, 262
ordinary files, 6 t
4 standard I/O
O (input/output), 28– i
3 30
’ t
– output delimiters
R u
1 paste command, 69–
e t
4 70
il i
4 sort command, 88
o
l
n
y
l P ${parameter}, 239–
o
&
packages, Cygwin, 240
g A 360 ${parameter:+ value},
i s parameters. See 242
s also variables ${parameter:= value},
241
${parameter:-
value}, 240
${parameter:?
value}, 241–
242
overview, 324–
325
pattern
matching,
242–244
p
o
s
i
t
i
o
n
a
l

p
a
r
a
m
e
t
e
r
s
d
e
f
i
n
i
t
i
o
n

o
f
,

2
3
9
left-shifting,
128–129
overview, 322
pipe symbol (|) 381
f ter
reassig e, 123–
ning 124 r s,
values ${n} o 69
to,
239, special m –
247– variable, 70
248
128 d fro
setting
phonebo i m
, 350
ok file f sa
shiftin
g left, example, f me
352 124–128 e file
substit positiona r ,
ution,
l e 70
121–
122 paramet n PATH variable,
special ers, 121– t 217–224, 324
paramete 122 p
rs, 323–
324 shift f a
comman
parent i t
d, 128–
processes, 129 l h
257
vari e n
pare
ables s a
nthe
to , m
ses
subshe e
in lls, 6 s
test 234– 8 ,
com 235 –
man pass 6
d, 1
words 9
144 0
,
pars –
enteri o
ing 1
ng at u
pha 2
login, t
se, 40 p
44 paste u p
passing com t a
argument t
mand
s
-d option, d t
$# 69–70 e e
variabl
e, overview, l r
122– 68–69
i n
123 -s option, 70
m
$* pasting lines
variabl i
m
a case ,
command,
-l option, 82–83
t 155–156, -n option, 83
c 336–337 5
overview, 78–81
h character 3
regular expressions,
sets, 55–57
i – 81–82
d 5
n -v option, 82
u 4
g match
p file
a ed
l na
n charac
i me
y ters,
c sub
c saving,
a stit
h 61–63
t utio
a overvi
e n,
r ew, 51
a 25– parameter substitution,
e 27 242–244
c
n gre precis
t
t e
e p
num
r com
r, ber
i ma of
5
e nd subp
1
s atter
– ns,
,
5 59–
61
3
8 summ
b
9 ary
e of

g regul
9
i ar
0 expr
n
essio
n n
e
i char
n acter
n
d s,
g 63–
o 64
o
f zero or more
f characters, 57–59
li
n P
l
e e
i
, a
n
5 r
e
3 s
o a period (.) 7,
n t dot (.) command, 27
227–230, 334–335
c 7
pattern matching,
b h dis
51–53, 63
o i pla
perl command, 91
o n yin
phon
k g ebo g

s , ok ent
file. rie
,
See
2 s
also
4 rolo fro
3
2 (Rol m,
6
– ode 27
1 x)
2 8–
pro
4 gra 27
p
3 m 9
e
a edi
r
w d tin
c
i d g
e
t i ent
n
h n rie
t
g s
p in,
s r 28
e
i i 1–
n
g n t 28
n t r 3
f i list
( e ing
% c s ent
) o rie
job control, 315 m t s
p m o in,
a a , 28
t n 3–
t d 1 28
e , 2 4
r 5 looking up entries in,
124–125, 278
n 2 –
remov
0 1
ing
m 2 2 entri
e ,
s

f 1
r 9
o 9
m
,
1 p
2 i
7
p

1 e
2
8
s
,
2 y
8 m
0
b

2 o
8 l
1
P
(
I
|
D
)
s || construct, 161–162
case command,
( 159–160
p loops, 178–179
r pipeline process,
33–34, 49, 321,
o
322
c
e
s
s

I
D
s
)
,

3
7
382 plus sign ()
prece
plus sign n, 121–122 d
(+) dence
POSIX shell a
job of
compatibility t
control, opera
summary,
315 e
319–320
tors,
pattern /
overview, 1
matching, 330 t
63 startup, 321
precise i
printf subshell number of
format execution, m
subpatterns,
specificati 332 e
matching,
on vi
modifier, 59–61 ,
206 line precisi
poi edit on 5
nte mo ,
modifi
rs de,
er
to 326 9
(printf)
vari – 5
, 205–
abl 329 –
206
We 9
es, printf
b 6
25 comm
doc
7 and
um f
pos example,
ent 206–207 o
itio
atio format r
nal
n, specification m
par
360 characters, a
am
pound sign 202–205 t
ete (#)
format t
rs comments, 96
specification e
definition pattern
of, 239 matching, modifiers, d
left- 243 205–206
shifting, printf format syntax, 202 o
128–129 specification
modifier, 206 printing u
overview,
322 PPID command t
information,
reassignin variable, 324 p
356
g values to, prarg u
239, 247–
248 s t
setting, progr example, 206–207
350 fo
am,
shifting r
169–
left, 352 m
170 a
substitutio
t t s aitin
i (P g for
s o
ID com
p n
s) pleti
e
c m , on
i o 3 $! variable, 257–258
f d wait command, 257
i i 7,
c f 1 .profile file, 235–236
a i 9 on program, 132–
t e 135, 145–147,
i r 9 170–171
o s pr programs
n , o add, 125–127, 277
c addi, 200
c 2
h 0 e a
a 5 ss r
r – e g
a 2
s s
c 0
t 6 definition of, 43 ,
e printf command parent/child, 257
r syntax, 202 re 1
s
list of active jobs, 347 tu 2
,
t r 2
2 o ni –
0 w n 1
2 or g 2

ki in 3
2
0 ng fo
5 di r a
f re m r
o ct
at g
r
or u
m io
a y,
n m
t 34
a e
8
b n
s pr
p o t
o s
e ut
c c , ,
i e 3
f
ss 7 p
i
c ID w a
a
s bo
s ok
i file
n ex
g am
$# variable, ple
122–123 ,
$* variable, 12
123–124
4–
$
12
{
8
n
po
}
siti
on
s al
p pa
e ra
c me
i ter
a s,
l 12
1–

v 12

a 2

r shift command,
128–129
i
cdtest, 225
a
change, 281–283
b
command files, 93–
l
96
e
comments, 96
,
ctype, 155–156, 158–
159

1
2
8

p
h
o
n
e
PWD variable 383
311
db, h initial
227 rolo
e code
– (Rol
229 l listing,
odex
deb l 185–
uggi )
s 187
ng, $$ variable, 198–
157 greetings, revised
149–151, 199
– code
159 159–160 add program, 277
init, 40–43 listing,
disp change program,
1 281–283
lay, ison, 122
278 8
listall, 283– 8 data formatting,

284 – 273–274
279
lu, 124–125,
1 display program,
e 9
278 278–279 final
x 0
m code listing,
e n
y u 274–277 fun,
c
b m
270–271
u b
a e initial code listing,
t
s r
193–194 listall
i ,
e program, 283–
o
n 1 284
n 5
a 4 lu program, 278
,
m – PATH variable, 221–
1
e 224
4 5
, 5 rem program, 280–
5 281
2 number2,
– 253
4
4 on, 132–
4
7 135, 145–
147, 170–
171
m
g prargs, 169–
y
e 170
c
t rem, 147–
p 148, 151–
t 153, 280–
echo
y 281
escape
. charact r
ers, e
187– v
S
188
e e
final
e code r
listing, s
190–
s e
193
,
r i a 00
e l b expor
v e l ted
i , e varia
s s bles,
e arithmeti 332,
2
c
d 6 340–
expansio
4 n, 103– 341
104 filename
c –
assigning substitution, 101–
o 2 values to, 103
d 6 97, 322, HISTSIZE, 326
333
e 6 null values, 100–
definitio 101
sample
n of, 97
output, readonly variables,
l 284–287 d 349
i sample i table of, 323–324
s runs, s
195–196 undefined variables,
t p 100–101
temporar
i y files, l stats, 95–96
n 198–199 a trace
g run, 95, 121, y mode,
164
, i turning
s
n on/off,
h
1 g 246
a
9 twhile,
r
6 169
v
,
– vartest, 209
a
1 vartest2, 210
l
2
9 vartest3, 212
u
6
8 vartest4, 213–214
e
7
waitfor, 171–174, 180–
s
184
r s words, 249–250
o h o ps command, 37
l e f PS1 variable, 216,
o l , 324
s l PS2 variable, 216,
324
u
9
b PS4 variable, 324
v 8
s pseudo-terminals, 40
a –
pseudo-tty, 40
r 1
pwd command, 12,
f i
95–
96,
348
PWD
variab
le,
324
384 question mark (?)
ks
e 11
r 1
la
v ov
Q sh
erv
(\ i
e ie
)
question w,
mark (?) e w
s 32
filename ,
substitutio c 9
n, 25–27, a single
47, 331 p 1 quote
pattern i
1 s ('),
matching, n
242, 336 g 1 105–
quote – 108
characters c
1 smart
h
b
a 1 quote
a r 2 s, 119
c a
k c
t d
e
q o
r
u s u R
b
o
w l
t i r command,
e 301–303
e t
h -
, r
( q
` 1 u
) 1 o f
, 1 t i

e
1 l
1 1 s
e
1 4
4 inside
(
double o
– "
quotes, p
1 )
1 112– e
,
5 114 line r
continu a
1
b ation, t
0
a 112 o
9
c o r

v ,
m final
reading data
1 a code
exit status, 199–202
4 n listing
menu-driven phone
2 d , program (rolo)
exit status, 190– $$ variable, 198–199

199–202 193
1 ini
menu-driven initial
4 tia
phone
program code l
3
(rolo) listing co
$$ , 185– de
r variable,
198–199 187 list
a revise in
initia
c d g,
l
e code 19
code
listin listing 3–
c g, , 188– 19
193– 190 4
o
194 overview, re
n
348–349
d revis vis
syntax, 185
i ed ed
code co
t
listin de
i
g, list
o
196– in
n
198 g,
s
sam 19
,
ple 6–
runs, 19
1 195– 8
9 196 sa
9 temporary m
files, 198– ple
199
r ru
mycp
program ns,
e
echo 19
a
escap 5–
d
e 19

chara 6
c temporary files,
cters,
o 198–199
187–
m mycp program
188
e e 1 read-only variables,
c 8 254, 349

h l 5 reassigning
values to
o i –
positional
s 1 parameters
e t 8 , 239, 247–
s i 7 248

c n redirection (I/O)

a g r <&- construct, 262

p , e >

e v &

1 i -

c 9 s
h 0 e c

a – d o

r 1 n

a 9 c s

c 3 o t

t d r

e i e u

r n c

s i l t

, t i ,

i s
1 a t 2

8 l i 6

7 n 2

– c g
1 o , i

8 d n

8 e 1 p

8 u

f l 8 t

i i –
n s 1 r

a t 9 e

l i 0 d

n read command i
syntax, 185 r
c g
o , readonly e
command, 254,
d c
349
t ut/out 331–332
i put), POSIX shell, 331–
332
o 28–30
in programs, 94
n l
< (left arrow), o
331
o
exec
p
command,
230–231, 262 s
in- ,
line
inp 1
ut 7
red 7
irec –
tio 1
n, 7
262 8

267 o
ove u
rvie t
w, p
331 u
POSIX shell, t
331
shel r
l e
arc d
hiv i
e r
cre e
atio c
n, t
264 i
– o
267 n
sta exec command,
nda 230–231, 262
rd overview, 30–32
I/O standard output,
closing, 262
(inp
overview, 48–49,
running rolo program 385
boo re
standard e
k, 3 p
error, f
reg co
writing to, e
ular m
261–262 r
exp m
standard e
res a
I/O n
sio n
(input/outp c
ns d,
ut), 28–30 e
[.] construct, 8
redirectio s 55–57 1
n append books, 360– \(.\) –
(>>) 361 construct,
61–63 8
character online
documentati 2
\{.\}
s, 31–32 on, 359 construct, overview, 51
r overview, 59–61
period (.), 51–53
359
e asterisk (*),
summary table,
Web 57–59
- 61–63
documentati caret (^), 53
e on, 360 rem program,
d 147–148, 151–
n ref
o 153,
t ere 280–281
l
r nci removing
l
y ng aliases, 309
a
job characters from
r
o s, input stream,
f 333 77–78
s
– directories, 22–
i
c 334 23
g
o reg duplicate lines
n
m ex. sort
command,
m See ( 84
a reg $ uniq
n ular ) command,
88–89
d exp ,
files, 9
s res
function
, sio 5 definitions, 271
ns 3 lines, 73
3 regi – phonebook file
2 ster 5 entries, 127–
ing 4 128, 280–281
6
you
r r g
renaming command, 271, r a
files, 8–9 349
d n
r r
e d
e e
r ,
s v
,
e e
2
t r
8 2
t s
5 –
i e
2
n
3
g R
p
i
r
t r
t o
c o
r g
h l
a r
i o
p a
e
s m
,
, , (
R
D o
2 3
e l
6 1
n o
1 1
n d
i e
r r
s x
e e
, )
s v
o e
1 p
u r
rm command, 9 r
r s
r o
c i
m g
e n
d r
s g
i a
books, 360–361
r m
online
s $$ variable, 198–199
documentation
, 359 o add program, 277
c
overview, 359 r change program, 281–
o
Web t 283
documentation, m data formatting, 273–
360
m 274
return o
d n e 8
i g 4
s , l lu program, 278
p i PATH variable, 221–
224
l 2 s
r
a 7 t
e
y 4 i
m
– n
p 2 g
r 7 , p

o 7 r

g 1 o

r f 9 g

a u 3 r

m n – a

, c 1 m

t 9 ,

2 i 4
7 o 2

8 n l 8

– s i 0

2 , s –

7 t 2

9 2 a 8

7 l 1

f 0 l
i – r

n 2 p e
a 7 r v
l 1 o i
i g s
c n r e
o i a d
d t m
e i , c
a o
l l 2 d
i 8 e
s c 3
t o – l
i d 2 i
) i
s
p n
t r
i g
o
n g
r
g r
a
, m o
196–198
rolosubs file, l
rolosubs file, 264–266
264–266 o
r
sample output,
284–287 u
p
sample runs, n
195–196 r

temporary files, o
198–199 p g
R r r
o o a
l
g m
o
d r ,
e a
x m
1
p ,
9
r
5
o
g 9 –
r 5 1
a , 9
m
. 6
1
S
2
e
e 1
,
r
o
l 1
o 6
4
(
R
o
r
l
o u
d n
e
n
x
386 -s file operator
c 9–
ing
e 11
sear
s 1
S chin
sin
g
gle
-s file files o
qu
operator, with f
142–143 ote
grep
%s s
-l option, 82–
for 83 c ('),
ma h 10
-n option, 83
t
a 5–
spe overview, 78–
81 r 10
cifi
cati regular a 8
on expressions,
c set command
81–82
cha -- option, 248–
rac -v option, 82 t
250
ter, sed e
IFS variable, 251–
20 command r 254
3 d command,
s m
saving 73
matched o
examples, 73
characters n
-n option, 72 d
, 61–63
i
overview, 70– o
scanning t
72 u
command
semico o
line twice b
before lon (;), r
l
executing 36, e
, 255–257 o
179–
search p
180,
order, 319 q
321 t
se u
sendin i
ar o
g o
ch t
comm n
in e
ands ,
g. s
to
Se
backgr 3
e
ound, ( 3
pa "
36–37 1
tte )
se
rn ,
q w
m
u i
at
e 1 t
ch h
n 0
2 HISTSIZE, 326
shell variables. See
n also parameters 9 HOME, 217
o arithmetic 0 IFS, 251–254
expansion, 103–104 – listing, 247
a assigning values to, 2 local variables, 209–210,
97, 322, 333 303
r 9
definition of, 97 n
g 1
d u
u exported variables,
i 211–216, 332, l
m
s 340–341 l
e
p fi
n l
l v
t e
a n a
s
y a l
,
i m
u
e
n e
2
g s s
4
u ,
7
v b
overview, s
239, 321, a 1
t
350–351 l i 0
positional u t
0
paramete u
e –
rs, t
reassigni s i 1
ng, 247– o 0
248 o n
1
-x option, f
246 a
, n
shar p
program, d
a
267 9 ,
s
shell 8
archives, 1 s
– 0
creating, i
264–267 1 1
n

0
1 g
0 0
3
t
E finding
o
N number
of
V
charact s
, ers u
stored
b
in, 244
9
s
h
s
e
p
l
e
l
c
s
i
,
234–235 a

PATH, 217–224 l

pointers, creating,
257 v
PS1, 216 a
PS2, 216 r
r i
e a
a b
d l
- e
o s
n $? variable, 132–
l 135
y $! variable, 257–
258
$# variable, 122–
v 123
a $* variable, 123–
r 124, 166
i $@ variable, 166–
167
a
${n} variable, 128
b
l
$0 variable, 245
table of, 323–324
e
s TERM, 236–237

, TZ, 236–237
undefined variables,
100–101
2
unsetting, 254
5
4
,

3
4
special variables 387
9 nts, 259–
shells shell. See
– 260
Bash. See POSIX shell
also 2
responsibiliti
nonstanda 9
rd shell es of
features environ 0
compatibi ment
lity control,
s
summary, 49
319–320 u
filenam
history b
e
of, 289 s
substitu
Web h
docum tion, 47
e
entatio interpre
n, 360 l
ted
compatib l
progra
ility s
mming
summary languag definitio
e, 50 n of, 43
, 319–
I/O environ
320 ment,
redirectio
current n, 48–49 210–211
shell, overview, overview,
44–45 227
executin
pipelines, terminating,
g files in,
340
227–230 49
typi
definition program
of, 1 execution ng
, 45–47 comma
Korn shell.
See also variable nds to,
nonstand substituti
on, 47 43–44
ard shell
features s shift
compa p comm
tibility and,
e
summa
ry, c 128–
319– i 129,
320
f 352
history
of, 289 y signal

Web i s
docum n handling
entatio g
with trap
n, 360 command
,
login executio
shell, 40– 2 n with
43 8 no
POSIX argume
ignored signals, i n d
260 g -k2n option, 87
n
overview, 258– -n option, 86
g
259
c s -o option, 85
signal numbers,
258 o o other options, 88
trap reset, 261 m r overview, 84
ignoring, 260 m t -r option, 85
numbers, 355 a , -t option, 87–88

s n -u option, 84

i d 8 sorting lines
s 7 arithmetically, 86
n
s d
g
i m e
l
n a li
e
r m
l t it
q
o e
u
o q r
o
p u c
t
s o h
e
, t a
s
e r
1 s a
(
7 c
,
'
6 t
)
– e
1
,
1 r
1
7 s,
9
1 7 8
0 7
s
5 f –
o
– i 8
r
1 e 8
t
0 l d
8 d u
c
s p
o
s li
d m c
k
u m a
i
r a t
p
i n e
p
l overview, 84 s
i reverse order, 85 , 6
n s
e k s
2
s i p
7
, p e
p c
e e s
i
l d p
a
i a
l
m f r
i i s
v
n e e
a
a l r
t d a i
i s
r a
n ,
r b
g
a l
,
8 y e
7 s s
8
, $? variable, 132–135
4
s $! variable, 257–258

p $# variable, 122–123
t 3
$* variable, 123–124,
o a 1 166
c 0 $@ variable, 166–167
o e s
u s p
t e
p
i c
u
n i
t
a
l
f f
i i
f
l l
i
e e
l
, n
e
a
s
8 m
,
5 e
388 special variables
m AND operator
${n} d
variable, m (-a), 143–144
128 e logical
$0 e
n negation
variable, r
245 t operator (!),
r
table of, s 143 logical
323–324 o
, OR operator
s r
(-o), 144
p overview, 35
9 overview, 135
e writing to,
261–262 6 string
c operators,
standard I/O 135–139
i (input/output i
f ) f
y closing, 262 elif
deleting from construct,
i
148–151
input stream,
n
else
77–78
g construct,
overview, 28– 145–147
30 exit
s
redirecting, command
h 230–231 , 147–148
e redirection, exit status,
l 261–262 131–135
translati nesting,
l
148–149
, ng
syntax,
characters
131
from, 74–77
2 testing
standard condition
8
shell. See s in, 131–
9 144
POSIX
– testing
shell conditions in
2
9
starting alternativ
up POSIX e format
0
shell, 321 for test,
statemen 139–140
s
ts file
t
&& operators,
a construct, 142–143
n 161–162
integer
d || construct,
operators,
161–162
a 140–142
c
r logical
o
s 132 u command overview, 70–
72
t zero values, 131 e
d command, 73
a s s
examples, 73
t t
-n option, 72
s o i
string operators, 135–
p n 139
p subpatterns,
p
e v matching, 59–61
r
d a s
o
r u
g
j i b
r
o a s
a
b b c
m
s l r
,
, e i
s p
9
3 , t
5
1 s

6 9 ,
9
– 7
6
3 3
1 s 0
s 7 t 9
t , r
a e
s
t 3 a
u
u 3 m
b
s 4
s
, e
h
s d
e
e t i
l
x o t
l
i r o
s
t i r
(.) construct, 231–234
$? variable, 132– n
{
135 g (
definition of, s
321 .
v e
non-zero values, ;
131 a d
overview, 131– l )
} u n
(.) c i
c co t t
o m , i
n m o
s an 1 n
t d, 1
r 22 5 o
u 7– – f
c 23 1 ,
t 0 1
, en 8 1
vi 1
2 ro b 2
3 n a expr command,
1 m c 119–120

– en k f
i
2 t, l
3 21 q e
n
4 0– u a
21 o m
e
d 1 t
e exec command, e s
227–230 u
f b
execution, 332 s
i (
overview, 227 t
n ` i
passing variables to, t
i )
234–235 u
t , t
substitution
i i
command o
o substitution 1 n
n $ 1 a
( 4 s
o – t
.
e
f ) 1 r
, 1 i
s
c 5 k
4 o
3 d (
n
*
d s e )
o t f ,
2
t r i 4

2
5
overview, 47
POSIX shell,
331
text 389
symbolic program, 340
q
links, 21–23 test command
u 1
alternative
e 0
format, 139–
s 1 140
t – T file operators,
142–143
i 1
integer
o 0
temporary operators,
n 3 files, 198– 140–142
parameter 199 logical AND
substitution TERM, 236–
m operator (-
$ 237, 258
a a), 143–144
{paramete t
r r}, 239– logical
240 e
k negation
$ r
operator
{paramete m
( (!), 143
r:+
? i
value}, logical OR
) 242 n
operator (-
, $ a o), 144
{paramete l
r:= overview,
2 value}, , 135, 352–
5 241 354
– $
2 parentheses,
{paramete 144
2 r:-value}, 8
7 240 string
operators,
$ 135–139
{paramete t
s syntax, 135
r:?value}, e
h 241–242
e r
overview,
l 324–325 m

l pattern i
matching, n
242–244
v a
positional
a parameters, t
121–122 i
r
tilde n
i
substitution,
a 318–319, g
329
b functions,
variable 271
l
substitution, jobs, 315
e 47, 98–100
loops, 336
s suspending
jobs, 316 shell
,
testing ator o
conditions in if (!), t t
statements
143 e e
alternative format,
139–140 logi s s
file operators, cal t
142–143 OR (
integer operators, ope c "
140–142
rato o )
lo
r (- m ,
gi
o), m
ca
144 a 1
l
over n 0
A
vie d 9
N
w, –
D
135 s 1
o
parentheses, 144 y 1
p
s n 1
er
t t
at
r a s
or
i x i
(-
n , n
a)
g g
,
1 l
1
o 3 e
4
p 5
3
e text q

r ASCII u
1
a characters, o
4
t octal values t
4
o of, 75 e
lo
r character s
gi
s sequences
ca
, d (
l
o '
n
1 u )
eg
3 b ,
at
5 l
io
– e 1
n
1 0
o
3 q 5
p
9 u –
er
1 1 r
0 1 n o
8 – f
c 1 m
u 1 a l
t 2 t i
t c n
i f h e
n i i ,
g l n
, e g 5
n a 3
6 a n
4 m y c
– e h
6 s c a
6 allowed h r
characters, 6
a a
special
d r c
characters, 28
e a t
line continuation,
l 112 c e
e p t r
t a e
i s r s
n t , e
g i t
with sed, 73 n 5 s
with tr g 1 ,
command, 77– –
,
78
5 5
echoing, 6
6 3 5
e
8 –
s
– b 5
c
7 e 7
a
0 g
p
i e
i
p n n
n
a n d
g
t i
,
t n o
e g f
1
p
l a
r
i
a
n m
e e
, t
e
r
5
3 s
– u
b
5
s
4 t
filename i
substitution, t
25–27 u
m t
a i
t o
c n
h ,
e
d 2
4
c 2
h –
a 2
r 4
a 4
c
t
e
r
s
,

s
a
v
i
n
g
,

6
1

6
3
390 text
trap
prec e command U
ise zone,
execution %u
nu
deter with no
m format
minin argument
be
s, 259– specific
r g,
260 ation
of 237
ch ignored charact
time signals, 260
ar
er, 203
act s overview,
er 258–259 umask
com
s, signal comma
man
59 numbers, 258 nd, 356
– d,
trap reset, 261 u
61 354,
Trojan horse, n
zero 355 218–219
or a
more – true
command, l
charac 356
ters, 356 i
57–59 tool
turning a
sorting, s.
on/of s
84–88 See
trace
translat com
ing mode,
man c
from 246
stand ds o
twhile
ard tr command m
input,
progra
-d option, m
74– 77–78 m, 169
a
77 type
examples, 78 n
Thompson command,
octal values 271
, Ken, 1 d
of ASCII types,
tilde ,
characters, integer, 304–
subs 305
75
titut typing
overview, 3
ion, loops
74–76 0
318 on one
-s option, 9
– 76–77 line, ,
319, trace mode, 179–
329 turning
180 TZ
on/of, 246 3
time variabl
translating 5
printing,
characters e, 236–
5 6
from 237
t
standard
i input, 74– u
m 77
n
a neg q books, 360–361
r atio online
documentation, 359
y n
c
overview, 359
oper
o
Web
ator documentation, 360
f m
(!), strengths of, 1
i m
143 unix.org website, 360
l a
und unset command,
e n
efin 254, 271, 357
d
ed until command, 170–
-c option, 90
o 174, 357
vari -d option, 89–90
p users,
able overview, 88–89
e returnin
s, Unix
r g
100 d
a informa
– e
t tion
101 v
o about,
u e
r 5–6
n l
s utilities,
d o
, 39. See
e p
also
r m
1 comma
s e
4 n nds
c
2 o t
– r
V
1 e o values
4 f assigni

3 , ng to
(
u variabl
_
n 1 es, 97
)
a reassig
,
r ning to
r
e positio
y
3 s nal
l
2 o param
o
2 u eters,
g
r 239, 247–248
i
c variables (shell). See
c u also parameters
e
a n arithmetic expansion,
s 103–104
l i
assigning values
to, 97, 322, 333
definition of,
97
displaying
values of, 98–
100
writing to standard error 391
166 vartest2
ENV, read-only
program, 210
290–291 variables, $@
254 variable, vartest3
exported
variables, r 166–167 program, 212
211–216, ${n}
e
332, variable,
340–341 a 128
filename d $0
substituti o variable,
on, 101– 245
103 n
substitution,
finding ly
47, 98–100
number v
table of,
of a 323–324
charact
ri TERM, 236–
ers
237
stored a
in, 244 TZ, 236–237
b
HISTSIZE, u
le
326 n
s, d
HOME,
217 3 e
4 f
IFS, 251–
i
254 9
n
listing, s e
247
p d
local
e
variables, v
209–210, ci
a
303
al r
null i
v
values,
a
100–101 a
b
passi ri l
ng to a e
subs b s
,
hells, le
234– s 1
235 $? 0
variable, 0
PATH
132–135 –
, 1
$!
217– variable, 0
224 257–258 1

pointers, $# unsetting,
creating, variable, 254
257 122–123
vartest
PS1, 216 $* program,
variable, 209
PS2, 216 123–124,
v a
a m n
r o d
t ,
d W
e e
-
s , 2
w
t 3 5
4 5 7
f
1 ,
i
p vi line edit mode
l
c 3
r
o e
5
o
m
8
g m o
a waitfor program,
r
p 171–174, 180–184,
n
a 232–234
d e
m waiting for job
r
, h completion
a
i $! variable, 257–258
s t
overview, 358
2 t o
o wait command, 257
1 r
r wc command, 7, 95–
3 y , 96
– , W
2 1 e
a
1 c 4 b
4 c 2
e

s d
v s 1
o
e i 4
c
n 3
r g u
b , m
o w
e
2 a
s n
9
e i
4 t
– t
a
2
s 9 t
c
h 6 i
o
e overview, 292–294, o
326–329, m
l n
351 m
l ,
360 , 5

3 F –
r 3 2
6
e 6 0
0
e 0 6
The Open Group, w
W 360
S o
e o
while loops, 168–
170, 358 r
b f
whitespace, 45, d
t 321 s,
E w who command, 5– c
d a 6
o
i r w
u
t e i
n
i d
ti
F t
o n
o h
n g,
u
7
n
o m w
d
f o o
a
d r
t
i d
b i
f s
o o
i
o n
e
k , p
r
, r
3 o
6 ( g
3
0 p r
r a
w K i m
e o n ,
b r t
s n f
2
i )
s 4
t ,
h 9
e
e –
s
2 2
Cygwin, 360 l
l
0 5
cygwin.com,
0

w
o
r
k
i
n
g

d
i
r
e
c
t
o
r
y
definition of, 10
displaying, 12
printing to, 348
writing to
standard error,
261–262
392 -x file operator
i
Y-Z t

X -
z s

-x file t
operator, s a
142–143
t t
%X format
specificati r u
on i s
character,
n ,
203
%x format g
specificati 1
on
character, o 3
203 1
p
-x zero or more
e
option, characters,
r matching, 57–59
debug
a
ging
t
with,
o
157–
r
159
,
xtrace
mode,
1
351
3
7
,

1
3
8

z
e
r
o

e
x

Vous aimerez peut-être aussi