Tải bản đầy đủ (.pdf) (506 trang)

The linux command line, 2nd edition

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (7.8 MB, 506 trang )

The second edition of this worldwide best seller
includes coverage of bash 4.x features like new redirection operators and shell expansions. An updated
shell scripting section discusses modern, robust shell
scripting practices and ways of avoiding common
types of potentially dangerous failures.
As with the first edition, you’ll learn timeless command
line skills like file navigation, environment configuration,
command chaining, and pattern matching with regular
expressions. You’ll even explore the philosophy behind
the many command line tools and the rich heritage that
your Linux machine inherits from Unix supercomputers
of yore—all handed down by generations of mouseshunning gurus.
As you work through the book’s short, easily digestible
chapters, you’ll learn how to:
• Create and delete files, directories, and symlinks

COPIES SOLD

2ND EDITION

• Administer your system, from networking and
package installation to process management
• Use standard input and output, redirection, and
pipelines
• Edit files with vi, the world’s most popular text editor
• Write shell scripts to automate common or boring tasks
• Slice and dice text files with cut, paste, grep, patch,
and sed
Once you’ve overcome your initial “shell shock,” you’ll
find that the command line is a natural and expressive
way to communicate with your computer.


ABOUT THE AUTHOR

William Shotts has been a software professional and
avid Linux user for more than 15 years. He has an
extensive background in software development, including
technical support, quality assurance, and documentation.
He is also the creator of LinuxCommand.org, a Linux
education and advocacy site featuring news, reviews,
and extensive support for using the Linux command line.

T H E L IN U X COMM A ND L INE

The Linux Command Line will take you from your first
terminal keystrokes to writing full programs in the latest
version of bash, the most popular Linux shell.

OVER 100,000

D N
N O
2 TI
I
D

E

BA NISH YOUR
MOUSE

THE LINU X

CO M M A N D L I N E
A

COMPLETE IN T RODUC T ION

WILLIAM SHOT TS

T H E F I N E ST I N G E E K E N T E RTA I N M E N T ™

$39.95 ($53.95 CDN)

SHOT TS

w w w.nostarch.com

SHELVE IN:
COMPUTERS/LINUX

www.allitebooks.com



The LInux Command Line

www.allitebooks.com


www.allitebooks.com



The Linux
Command Line
2 ND E d i t i o n
A Complete Introduction

b y W il l ia m S ho t t s

San Francisco

www.allitebooks.com


The Linux Command Line, 2nd Edition. Copyright © 2019 by William Shotts.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-10: 1-59327-952-3
ISBN-13: 978-1-59327-952-3
Publisher: William Pollock
Production Editors: Meg Sneeringer and Serena Yang
Cover Illustration: Octopod Studios
Developmental Editor: Chris Cleveland
Technical Reviewer: Jordi Gutiérrez Hermoso
Copyeditor: Kim Wimpsett
Compositors: Britt Bogan and Meg Sneeringer
Proofreader: James Fraleigh
For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 1.415.863.9900;

www.nostarch.com
The Library of Congress issued the following Cataloging-in-Publication Data for the first edition:
Shotts, William E.
The Linux command line: a complete introduction / William E. Shotts, Jr.
p. cm.
Includes index.
ISBN-13: 978-1-59327-389-7 (pbk.)
ISBN-10: 1-59327-389-4 (pbk.)
1. Linux. 2. Scripting Languages (Computer science) 3. Operating systems (Computers) I. Title.
QA76.76.O63S5556 2011
005.4'32--dc23
2011029198

No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other
product and company names mentioned herein may be the trademarks of their respective owners. Rather
than use a trademark symbol with every occurrence of a trademarked name, we are using the names only
in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The information in this book is distributed on an “As Is” basis, without warranty. While every precaution
has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any
liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or
indirectly by the information contained in it.


To Karen



About the Author
William Shotts has been a software professional for more than 30 years and

an avid Linux user for more than 20 years. He has an extensive background
in software development, including technical support, quality assurance,
and documentation. He is also the creator of LinuxCommand.org, a Linux
education and advocacy site featuring news, reviews, and extensive support
for using the Linux command line.

About the Technical Reviewer
Jordi Gutiérrez Hermoso is a coder, mathematician, and hacker-errant. He
runs Debian GNU/Linux exclusively since 2002, both at home and at work.
Jordi has been involved with GNU Octave, a free numerical computing environment largely compatible with Matlab, and with Mercurial, a distributed
version control system. He enjoys pure and applied mathematics, skating,
swimming, and knitting. Nowadays he thinks a lot about environmental
mapping, greenhouse gas emissions, and rhino conservation efforts.



Brief Contents
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv

PART I: LEARNING THE SHELL
Chapter 1: What Is the Shell? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Chapter 2: Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Chapter 3: Exploring the System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Chapter 4: Manipulating Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Chapter 5: Working with Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Chapter 6: Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Chapter 7: Seeing the World as the Shell Sees It . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Chapter 8: Advanced Keyboard Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Chapter 9: Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

Chapter 10: Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

PART II: CONFIGURATION AND THE ENVIRONMENT
Chapter 11: The Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Chapter 12: A Gentle Introduction to vi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Chapter 13: Customizing the Prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

PART III: COMMON TASKS AND ESSENTIAL TOOLS
Chapter 14: Package Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Chapter 15: Storage Media . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Chapter 16: Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Chapter 17: Searching for Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Chapter 18: Archiving and Backup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209


Chapter 19: Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Chapter 20: Text Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Chapter 21: Formatting Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Chapter 22: Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Chapter 23: Compiling Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309

PART IV: WRITING SHELL SCRIPTS
Chapter 24: Writing Your First Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Chapter 25: Starting a Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Chapter 26: Top-Down Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Chapter 27: Flow Control: Branching with if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Chapter 28: Reading Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Chapter 29: Flow Control: Looping with while/until . . . . . . . . . . . . . . . . . . . . . . . . . 375
Chapter 30: Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Chapter 31: Flow Control: Branching with case . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395

Chapter 32: Positional Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Chapter 33: Flow Control: Looping with for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Chapter 34: Strings and Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
Chapter 35: Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
Chapter 36: Exotica . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459

x   Brief Contents


Conte nt s in De ta il
Acknowledgments

xxiii

First Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Second Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv

Introduction

xxv

Why Use the Command Line? . . . . .
What This Book Is About . . . . . . . .
Who Should Read This Book . . . . .
What’s in This Book . . . . . . . . . . .
How to Read This Book . . . . . . . . .
Prerequisites . . . . . . . . . .
What’s New in the Second Edition .
Your Feedback Is Needed! . . . . . . .


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.

xxvi
xxvi
xxvii
xxvii
xxviii
xxviii
. xxix
. . xxx


PART I: LEARNING THE SHELL
1
What Is the Shell?
Terminal Emulators . . . . . . . .
Making Your First Keystrokes .
Command History . .
Cursor Movement . . .
Try Some Simple Commands .
Ending a Terminal Session . . .
Summing Up . . . . . . . . . . . .

3
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.

.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.

.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.

.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

3
4
4
4
5
6
6

2
Navigation7
Understanding the File System Tree . . . . .
The Current Working Directory . . . . . . . .

Listing the Contents of a Directory . . . . . .
Changing the Current Working Directory .
Absolute Pathnames . . . . . . . . .
Relative Pathnames . . . . . . . . . .
Some Helpful Shortcuts . . . . . . .
Summing Up . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.

. 7
. 8
. 9
. 9
. 9
. 9
11
12


3
Exploring the System
More Fun with ls . . . . . . . . . . . . . . . . .
Options and Arguments . . . . .
A Longer Look at Long Format .
Determining a File’s Type with file . . . . .
Viewing File Contents with less . . . . . . .
Taking a Guided Tour . . . . . . . . . . . . .
Symbolic Links . . . . . . . . . . . . . . . . . .
Hard Links . . . . . . . . . . . . . . . . . . . . .
Summing Up . . . . . . . . . . . . . . . . . . .

13
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

4
Manipulating Files and Directories
Wildcards . . . . . . . . . . . . . . . . . . . . . .
mkdir—Create Directories . . . . . . . . . . .
cp—Copy Files and Directories . . . . . . . .
Useful Options and Examples . .
mv—Move and Rename Files . . . . . . . . .
Useful Options and Examples . .
rm—Remove Files and Directories . . . . . .
Useful Options and Examples . .
ln—Create Links . . . . . . . . . . . . . . . . . .
Hard Links . . . . . . . . . . . . . . . .
Symbolic Links . . . . . . . . . . . . .
Building a Playground . . . . . . . . . . . . . .
Creating Directories . . . . . . . . .
Copying Files . . . . . . . . . . . . .
Moving and Renaming Files . . .
Creating Hard Links . . . . . . . . .
Creating Symbolic Links . . . . . .
Removing Files and Directories .
Summing Up . . . . . . . . . . . . . . . . . . . .

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

23
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


What Exactly Are Commands? . . . . . . . . . . . . . . . . . . . . . . .
Identifying Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
type—Display a Command’s Type . . . . . . . . . . . . . .
which—Display an Executable’s Location . . . . . . . . .
Getting a Command’s Documentation . . . . . . . . . . . . . . . . . .
help—Get Help for Shell Builtins . . . . . . . . . . . . . . .
--help—Display Usage Information . . . . . . . . . . . . . .
man—Display a Program’s Manual Page . . . . . . . . .
apropos—Display Appropriate Commands . . . . . . . .
whatis—Display One-line Manual Page Descriptions .
info—Display a Program’s Info Entry . . . . . . . . . . . .
README and Other Program Documentation Files . . .

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

5
Working with Commands

xii   Contents in Detail

13
14
15
16
17
18
21
22
22

24
26
26
26
27
28
28
29

30
30
31
31
31
31
32
33
34
35
37

39
40
40
40
40
41
41
42
42
44
44
45
46


Creating Our Own Commands with alias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48


6
Redirection49
Standard Input, Output, and Error . . . . . . . . . . . . . . . . . . . . . . . . . .
Redirecting Standard Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Redirecting Standard Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Redirecting Standard Output and Standard Error to One File .
Disposing of Unwanted Output . . . . . . . . . . . . . . . . . . . . .
Redirecting Standard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
cat: Concatenate Files . . . . . . . . . . . . . . . . . . . . . . . . . . .
Pipelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
uniq: Report or Omit Repeated Lines . . . . . . . . . . . . . . . . . .
wc: Print Line, Word, and Byte Counts . . . . . . . . . . . . . . . .
grep: Print Lines Matching a Pattern . . . . . . . . . . . . . . . . . .
head/tail: Print First/Last Part of Files . . . . . . . . . . . . . . . . .
tee: Read from Stdin and Output to Stdout and Files . . . . . . .
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

7
Seeing the World as the Shell Sees It
Expansion . . . . . . . . . . . . . . . . . . . .
Pathname Expansion . . . . . .
Tilde Expansion . . . . . . . . . .
Arithmetic Expansion . . . . . .
Brace Expansion . . . . . . . . .
Parameter Expansion . . . . . .
Command Substitution . . . . .
Quoting . . . . . . . . . . . . . . . . . . . . . .
Double Quotes . . . . . . . . . .
Single Quotes . . . . . . . . . . .
Escaping Characters . . . . . .
Backslash Escape Sequences .

Summing Up . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.

61
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.

8
Advanced Keyboard Tricks
Command Line Editing . . . . . . . . . . . . . . . . . . . . . . . .
Cursor Movement . . . . . . . . . . . . . . . . . . . . .
Modifying Text . . . . . . . . . . . . . . . . . . . . . .
Cutting and Pasting (Killing and Yanking) Text .
Completion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Searching History . . . . . . . . . . . . . . . . . . . . .
History Expansion . . . . . . . . . . . . . . . . . . . .
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

50
50
52
52
53

53
53
55
55
56
57
57
58
59
59

61
62
63
64
65
66
67
67
68
69
70
70
71

73
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.

74
74
74
75
76
77
77
79
80

Contents in Detail   xiii


9

Permissions81
Owners, Group Members, and Everybody Else . . . . . . . . . .
Reading, Writing, and Executing . . . . . . . . . . . . . . . . . . .
chmod: Change File Mode . . . . . . . . . . . . . . . . .
Setting File Mode with the GUI . . . . . . . . . . . . . .
umask: Set Default Permissions . . . . . . . . . . . . . . .
Some Special Permissions . . . . . . . . . . . . . . . . . .
Changing Identities . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
su: Run a Shell with Substitute User and Group IDs .
sudo: Execute a Command As Another User . . . . .
chown: Change File Owner and Group . . . . . . . .
chgrp: Change Group Ownership . . . . . . . . . . . .
Exercising Our Privileges . . . . . . . . . . . . . . . . . . . . . . . . .
Changing Your Password . . . . . . . . . . . . . . . . . . . . . . . . .
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.

82
83
85
88
89
90
91
92
93
94
95
95
97
98


10
Processes99
How a Process Works . . . . . . . . . . . . . . . . . . . . . . . . . .
Viewing Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Viewing Processes Dynamically with top . . . . . . .
Controlling Processes . . . . . . . . . . . . . . . . . . . . . . . . . . .
Interrupting a Process . . . . . . . . . . . . . . . . . . . .
Putting a Process in the Background . . . . . . . . . .
Returning a Process to the Foreground . . . . . . . .
Stopping (Pausing) a Process . . . . . . . . . . . . . . .
Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Sending Signals to Processes with kill . . . . . . . . .
Sending Signals to Multiple Processes with killall .
Shutting Down the System . . . . . . . . . . . . . . . . . . . . . . .
More Process-Related Commands . . . . . . . . . . . . . . . . . .
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

100
100
102
104
105
105
106
106
107
107
109
110

111
111

PART II: CONFIGURATION AND THE ENVIRONMENT
11
The Environment
What Is Stored in the Environment? . . . . . .
Examining the Environment . . . . .
Some Interesting Variables . . . . .
How Is the Environment Established? . . . . .
What’s in a Startup File? . . . . . . .
Modifying the Environment . . . . . . . . . . . .
Which Files Should We Modify? .
Text Editors . . . . . . . . . . . . . . . .

xiv   Contents in Detail

115
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

116
116
117

118
119
121
121
121


Using a Text Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Activating Our Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125

12
A Gentle Introduction to vi

127

Why We Should Learn vi . . . . . . . . . . . . . . . . . . . . .
A Little Background . . . . . . . . . . . . . . . . . . . . . . . . .
Starting and Stopping vi . . . . . . . . . . . . . . . . . . . . .
Editing Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Entering Insert Mode . . . . . . . . . . . . . . . . .
Saving Our Work . . . . . . . . . . . . . . . . . . .
Moving the Cursor Around . . . . . . . . . . . . . . . . . . . .
Basic Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Appending Text . . . . . . . . . . . . . . . . . . . . .
Opening a Line . . . . . . . . . . . . . . . . . . . . .
Deleting Text . . . . . . . . . . . . . . . . . . . . . . .
Cutting, Copying, and Pasting Text . . . . . . .
Joining Lines . . . . . . . . . . . . . . . . . . . . . . .
Search-and-Replace . . . . . . . . . . . . . . . . . . . . . . . . .

Searching Within a Line . . . . . . . . . . . . . . .
Searching the Entire File . . . . . . . . . . . . . . .
Global Search-and-Replace . . . . . . . . . . . . .
Editing Multiple Files . . . . . . . . . . . . . . . . . . . . . . . .
Switching Between Files . . . . . . . . . . . . . . .
Opening Additional Files for Editing . . . . . . .
Copying Content from One File into Another .
Inserting an Entire File into Another . . . . . . .
Saving Our Work . . . . . . . . . . . . . . . . . . . . . . . . . .
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

13
Customizing the Prompt

128
128
128
130
131
131
131
132
132
133
134

135
136
136
137
137
137
139
139
140
141
141
142
143

145

Anatomy of a Prompt . . . . . . . . . . . . . . .
Trying Some Alternative Prompt Designs .
Adding Color . . . . . . . . . . . . . . . . . . . .
Moving the Cursor . . . . . . . . . . . . . . . .
Saving the Prompt . . . . . . . . . . . . . . . . .
Summing Up . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.


.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.


.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.

.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.

.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.


.
.
.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

145
147
148
150
151
151

PART III: COMMON TASKS AND ESSENTIAL TOOLS
14
Package Management
Packaging Systems . . . . . . . . .
How a Package System Works .
Package Files . . . . . .
Repositories . . . . . . . .

.
.
.
.

.
.
.
.

.
.
.

.

155
.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.

.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.

.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.

.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.

.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.

.
.

.
.
.
.

156
156
156
157

Contents in Detail   xv


Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . .
High- and Low-Level Package Tools . . . . . . . . . . . .
Common Package Management Tasks . . . . . . . . . . . . . . . . .
Finding a Package in a Repository . . . . . . . . . . . . .
Installing a Package from a Repository . . . . . . . . . .
Installing a Package from a Package File . . . . . . . .
Removing a Package . . . . . . . . . . . . . . . . . . . . . .
Updating Packages from a Repository . . . . . . . . . .
Upgrading a Package from a Package File . . . . . . .
Listing Installed Packages . . . . . . . . . . . . . . . . . . .
Determining Whether a Package Is Installed . . . . . .
Displaying Information About an Installed Package .
Finding Which Package Installed a File . . . . . . . . . .
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.


15
Storage Media

157
158
158
158
159
159
160
160
160
161
161
161
162
162

165

Mounting and Unmounting Storage Devices . . . . . . . .
Viewing a List of Mounted File Systems . . . . .
Determining Device Names . . . . . . . . . . . . .
Creating New File Systems . . . . . . . . . . . . . . . . . . . .
Manipulating Partitions with fdisk . . . . . . . . .
Creating a New File System with mkfs . . . . .
Testing and Repairing File Systems . . . . . . . . . . . . . .
Moving Data Directly to and from Devices . . . . . . . . .
Creating CD-ROM Images . . . . . . . . . . . . . .

Creating an Image Copy of a CD-ROM . . . .
Creating an Image from a Collection of Files .
Writing CD-ROM Images . . . . . . . . . . . . . . . . . . . . .
Mounting an ISO Image Directly . . . . . . . . .
Blanking a Rewritable CD-ROM . . . . . . . . . .
Writing an Image . . . . . . . . . . . . . . . . . . . .
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

166
167
170
173
173
175
176
177
177
177
178
178
179
179
179
179
180

16
Networking181
Examining and Monitoring a Network . . . .
ping . . . . . . . . . . . . . . . . . . . . .
traceroute . . . . . . . . . . . . . . . . .
ip . . . . . . . . . . . . . . . . . . . . . . .
netstat . . . . . . . . . . . . . . . . . . . .
Transporting Files over a Network . . . . . . .
ftp .  . . . . . . . . . . . . . . . . . . . . . .

lftp—a Better ftp . . . . . . . . . . . . .
wget . . . . . . . . . . . . . . . . . . . . .
Secure Communication with Remote Hosts .
ssh    . . . . . . . . . . . . . . . . . . . . . .
scp and sftp . . . . . . . . . . . . . . . .
Summing Up . . . . . . . . . . . . . . . . . . . . .
xvi   Contents in Detail

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

182
182
183
184
184
185
186
187

188
188
188
192
193


17
Searching for Files
locate—Find Files the Easy Way . . . .
find—Find Files the Hard Way . . . . .
Tests . . . . . . . . . . . . . . . . .
Operators . . . . . . . . . . . . .
Predefined Actions . . . . . . .
User-Defined Actions . . . . .
Improving Efficiency . . . . . .
xargs . . . . . . . . . . . . . . . .
A Return to the Playground .
find Options . . . . . . . . . . .
Summing Up . . . . . . . . . . . . . . . . .

195
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

18
Archiving and Backup
Compressing Files . . . . . . . . . . . . . . .
gzip . . . . . . . . . . . . . . . . . .
bzip2 . . . . . . . . . . . . . . . . .
Archiving Files . . . . . . . . . . . . . . . . .
tar .  . . . . . . . . . . . . . . . . . . .
zip    . . . . . . . . . . . . . . . . . .

Synchronizing Files and Directories . . .
Using rsync over a Network .
Summing Up . . . . . . . . . . . . . . . . . .

196
197
198
200
202
204
204
205
206
208
208

209
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

What Are Regular Expressions? . . . . . . . . . . . . . . . . . . . . .
grep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Metacharacters and Literals . . . . . . . . . . . . . . . . . . . . . . . .

The Any Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Anchors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Bracket Expressions and Character Classes . . . . . . . . . . . . .
Negation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Traditional Character Ranges . . . . . . . . . . . . . . . .
POSIX Character Classes . . . . . . . . . . . . . . . . . . . . . . . . . .
POSIX Basic vs. Extended Regular Expressions . . . . . . . . . . .
Alternation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Quantifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
? —Match an Element Zero or One Time . . . . . . . . .
*—Match an Element Zero or More Times . . . . . . .
+—Match an Element One or More Times . . . . . . . .
{ }—Match an Element a Specific Number of Times .
Putting Regular Expressions to Work . . . . . . . . . . . . . . . . . .
Validating a Phone List with grep . . . . . . . . . . . . . .
Finding Ugly Filenames with find . . . . . . . . . . . . . .
Searching for Files with locate . . . . . . . . . . . . . . . .
Searching for Text with less and vim . . . . . . . . . . . .
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

19
Regular Expressions

210
210
212
213
213
217
219
222
223

225
226
226

227
228
228
230
230
230
231
234
235
236
236
237
238
238
239
239
240
241
241
242

Contents in Detail   xvii


20
Text Processing

245

Applications of Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Email . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Printer Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Program Source Code . . . . . . . . . . . . . . . . . . . . . . .
Revisiting Some Old Friends . . . . . . . . . . . . . . . . . . . . . . . . . .
cat     .   . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
sort   .   . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
uniq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Slicing and Dicing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
cut—Remove Sections from Each Line of Files . . . . . . .
paste—Merge Lines of Files . . . . . . . . . . . . . . . . . . . .
join—Join Lines of Two Files on a Common Field . . . . .
Comparing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
comm—Compare Two Sorted Files Line by Line . . . . . .
diff—Compare Files Line by Line . . . . . . . . . . . . . . . .
patch—Apply a diff to an Original . . . . . . . . . . . . . . .
Editing on the Fly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
tr—Transliterate or Delete Characters . . . . . . . . . . . . .
sed—Stream Editor for Filtering and Transforming Text .
aspell—Interactive Spellchecker . . . . . . . . . . . . . . . . .
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

21
Formatting Output

246
246
246
246
246
247
247
247
248
254
256
256
258
260
262
262
263
265
266
266
268
275
278

278

279

Simple Formatting Tools . . . . . . . . . . . . . . . . . . . . . .
nl—Number Lines . . . . . . . . . . . . . . . . . . . .
fold—Wrap Each Line to a Specified Length .
fmt—A Simple Text Formatter . . . . . . . . . . . .
pr—Format Text for Printing . . . . . . . . . . . . .
printf—Format and Print Data . . . . . . . . . . .
Document Formatting Systems . . . . . . . . . . . . . . . . . .
groff . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.

280
280
282
283
286
287
290
290
295

22
Printing297
A Brief History of Printing . . . . . . . . . . . . . .
Printing in the Dim Times . . . . . . . .
Character-Based Printers . . . . . . . .
Graphical Printers . . . . . . . . . . . .
Printing with Linux . . . . . . . . . . . . . . . . . . .
Preparing Files for Printing . . . . . . . . . . . . .
pr—Convert Text Files for Printing .

xviii   Contents in Detail

.
.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.

.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.

.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

298
298
298
299

300
300
300


Sending a Print Job to a Printer . . . . . . . . . .
lpr—Print Files (Berkeley Style) . . . .
lp—Print Files (System V Style) . . . .
Another Option: a2ps . . . . . . . . . .
Monitoring and Controlling Print Jobs . . . . .
lpstat—Display Print System Status .
lpq—Display Printer Queue Status .
lprm/cancel—Cancel Print Jobs . . .
Summing Up . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.

23
Compiling Programs
What Is Compiling? . . . . . . . . . . . . .
Are All Programs Compiled? .
Compiling a C Program . . . . . . . . . . .
Obtaining the Source Code . .
Examining the Source Tree . .
Building the Program . . . . . .
Installing the Program . . . . . .
Summing Up . . . . . . . . . . . . . . . . . .

301
302
302
303

305
306
306
307
307

309
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.

310
311
311
312
314
315
318
319

PART IV: WRITING SHELL SCRIPTS
24
Writing Your First Script
What Are Shell Scripts? . . . . . . . . . . . . . . .
How to Write a Shell Script . . . . . . . . . . . .
Script File Format . . . . . . . . . . . . .
Executable Permissions . . . . . . . . .
Script File Location . . . . . . . . . . . .

Good Locations for Scripts . . . . . .
More Formatting Tricks . . . . . . . . . . . . . . .
Long Option Names . . . . . . . . . . .
Indentation and Line Continuation .
Summing Up . . . . . . . . . . . . . . . . . . . . . .

323
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

25
Starting a Project
First Stage: Minimal Document . . . . . . . . . . . . . . . . .
Second Stage: Adding a Little Data . . . . . . . . . . . . . .
Variables and Constants . . . . . . . . . . . . . . . . . . . . .
Assigning Values to Variables and Constants .
Here Documents . . . . . . . . . . . . . . . . . . . . . . . . . . .
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

323
324
324
325
325
326
327
327
327
328

329
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.


.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.

.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.

.

.
.
.
.
.
.

330
331
332
334
336
338

Contents in Detail   xix


26
Top-Down Design
Shell Functions . . . . .
Local Variables . . . . .
Keep Scripts Running .
Summing Up . . . . . .

.
.
.
.


.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

339
.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

27
Flow Control: Branching with if
if Statements . . . . . . . . . . . . . . . . . . . . . . .
Exit Status . . . . . . . . . . . . . . . . . . . . . . . .
Using test . . . . . . . . . . . . . . . . . . . . . . . . .
File Expressions . . . . . . . . . . . . . .
String Expressions . . . . . . . . . . . .
Integer Expressions . . . . . . . . . . . .
A More Modern Version of test . . . . . . . . . .
(( ))—Designed for Integers . . . . . . . . . . . . .
Combining Expressions . . . . . . . . . . . . . . .
Control Operators: Another Way to Branch .
Summing Up . . . . . . . . . . . . . . . . . . . . . .


.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.

349
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.

28
Reading Keyboard Input
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.

.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.


.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.

.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

29
Flow Control: Looping with while/until
.
.
.
.
.
.

.

.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

350
350
352
352
354

356
357
358
359
361
362

363

read—Read Values from Standard Input .
Options . . . . . . . . . . . . . . . . .
IFS . . . . . . . . . . . . . . . . . . . . . .
Validating Input . . . . . . . . . . . . . . . . . .
Menus . . . . . . . . . . . . . . . . . . . . . . . . .
Summing Up . . . . . . . . . . . . . . . . . . . .
Extra Credit . . . . . . . . . . . . . . . . . . . . .

Looping . . . . . . . . . . . . .
while . . . . . . . .
Breaking Out of a Loop . .
until . . . . . . . . .
Reading Files with Loops .
Summing Up . . . . . . . . .

340
343
344
347

.

.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.

.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.


.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.

.
.
.
.

364
366
368
370
372
373
373

375
.
.
.
.
.
.

.
.
.
.
.
.

.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.

.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.


.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.

.
.
.
.
.

376
376
378
379
380
381

30
Troubleshooting383
Syntactic Errors . . . . . . . . . . . . . . . . . .
Missing Quotes . . . . . . . . . . .
Missing or Unexpected Tokens .
Unanticipated Expansions . . . .

xx   Contents in Detail

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

383
384
385
386


Logical Errors . . . . . . . . . . . . . . . . . . . . . . .
Defensive Programming . . . . . . . . .
Watch Out for Filenames . . . . . . . .
Verifying Input . . . . . . . . . . . . . . . .
Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Test Cases . . . . . . . . . . . . . . . . . . .
Debugging . . . . . . . . . . . . . . . . . . . . . . . . .
Finding the Problem Area . . . . . . . .
Tracing . . . . . . . . . . . . . . . . . . . . .
Examining Values During Execution .
Summing Up . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.

31
Flow Control: Branching with case
The case Command . . . . . . . . . . . . .
Patterns . . . . . . . . . . . . . . .
Performing Multiple Actions .
Summing Up . . . . . . . . . . . . . . . . .

.
.
.
.

.
.
.
.


.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.


.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.


387
387
388
389
390
391
391
391
392
394
394

395
.
.
.
.

.
.
.
.

.
.
.
.

.
.

.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.

.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.

.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

32
Positional Parameters


396
397
399
400

401

Accessing the Command Line . . . . . . . . . . . . . . . . . . . . .
Determining the Number of Arguments . . . . . . . .
shift—Getting Access to Many Arguments . . . . . .
Simple Applications . . . . . . . . . . . . . . . . . . . . .
Using Positional Parameters with Shell Functions .
Handling Positional Parameters en Masse . . . . . . . . . . . .
A More Complete Application . . . . . . . . . . . . . . . . . . . .
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

33
Flow Control: Looping with for

401
402
403
404
405
405
407
410

413


for: Traditional Shell Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
for: C Language Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417

34
Strings and Numbers
Parameter Expansion . . . . . . . . . . . . . . . . . . . . .
Basic Parameters . . . . . . . . . . . . . . . . .
Expansions to Manage Empty Variables .
Expansions That Return Variable Names .
String Operations . . . . . . . . . . . . . . . . .
Case Conversion . . . . . . . . . . . . . . . . .
Arithmetic Evaluation and Expansion . . . . . . . . . .
Number Bases . . . . . . . . . . . . . . . . . . .
Unary Operators . . . . . . . . . . . . . . . . .
Simple Arithmetic . . . . . . . . . . . . . . . . .

419
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.

419
420
420
422
422
425
427
427
428
428

Contents in Detail   xxi


Assignment . . . . . . . . . . . . . . . . . . . .
Bit Operations . . . . . . . . . . . . . . . . . .
Logic . . . . . . . . . . . . . . . . . . . . . . . .
bc—An Arbitrary Precision Calculator Language .
Using bc . . . . . . . . . . . . . . . . . . . . . .
An Example Script . . . . . . . . . . . . . . .
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . .
Extra Credit . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


429
431
432
434
434
435
436
436

35
Arrays437
What Are Arrays? . . . . . . . . . . . . . . . . . . . . . . . . .
Creating an Array . . . . . . . . . . . . . . . . . . . . . . . . .
Assigning Values to an Array . . . . . . . . . . . . . . . . .
Accessing Array Elements . . . . . . . . . . . . . . . . . . .
Array Operations . . . . . . . . . . . . . . . . . . . . . . . . .
Outputting the Entire Contents of an Array .
Determining the Number of Array Elements .
Finding the Subscripts Used by an Array . . .
Adding Elements to the End of an Array . . .
Sorting an Array . . . . . . . . . . . . . . . . . . .
Deleting an Array . . . . . . . . . . . . . . . . . .
Associative Arrays . . . . . . . . . . . . . . . . . . . . . . . .
Summing Up . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.

437
438
438
439
441
441
441
442
442
443
443
444
444

36
Exotica447
Group Commands and Subshells . .
Process Substitution . . . . .
Traps . . . . . . . . . . . . . . . . . . . . . .
Asynchronous Execution with wait . .
Named Pipes . . . . . . . . . . . . . . . .
Setting Up a Named Pipe .
Using Named Pipes . . . . .
Summing Up . . . . . . . . . . . . . . . .

.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

447
451
453
456
457
458
458
458


Index459

xxii   Contents in Detail


Acknowledgments

I want to thank the following people, who helped make this book possible.

First Edition
First, the people who inspired me:
Jenny Watson, acquisitions editor at Wiley Publishing who originally
suggested that I write a shell scripting book.
John C. Dvorak, noted columnist and pundit. In an episode of his video
podcast, Cranky Geeks, Mr. Dvorak described the process of writing: “Hell.
Write 200 words a day, and in a year, you have a novel.” This advice led me
to write a page a day until I had a book.
Dmitri Popov wrote an article in Free Software Magazine titled “Creating
a book template with Writer,” which inspired me to use OpenOffice.org
Writer (and later, LibreOffice Writer) for composing the text. As it turned
out, it worked wonderfully.
Next, the volunteers who helped me produce the original, freely distributed version of this book (available at LinuxCommand.org):
Mark Polesky performed an extraordinary review and test of the text.


×