Table Of ContentB C L
ash ommand ine
and
s s
heLL Cripts
P P
ocket rimer
LICENSE, DISCLAIMER OF LIABILITY, AND LIMITED WARRANTY
By purchasing or using this book and disc (the “Work”), you agree that this license
grants permission to use the contents contained herein, including the disc, but
does not give you the right of ownership to any of the textual content in the book /
disc or ownership to any of the information or products contained in it. This license
does not permit uploading of the Work onto the Internet or on a network (of any
kind) without the written consent of the Publisher. Duplication or dissemination of
any text, code, simulations, images, etc. contained herein is limited to and subject
to licensing terms for the respective products, and permission must be obtained
from the Publisher or the owner of the content, etc., in order to reproduce or
network any portion of the textual material (in any media) that is contained in the
Work.
Mercury Learning and inforMation (“MLI” or “the Publisher”) and anyone
involved in the creation, writing, or production of the companion disc, accompany-
ing algorithms, code, or computer programs (“the software”), and any accompany-
ing Web site or software of the Work, cannot and do not warrant the performance
or results that might be obtained by using the contents of the Work. The author,
developers, and the Publisher have used their best efforts to insure the accuracy
and functionality of the textual material and/or programs contained in this pack-
age; we, however, make no warranty of any kind, express or implied, regarding
the performance of these contents or programs. The Work is sold “as is” without
warranty (except for defective materials used in manufacturing the book or due to
faulty workmanship).
The author, developers, and the publisher of any accompanying content, and any-
one involved in the composition, production, and manufacturing of this work will
not be liable for damages of any kind arising out of the use of (or the inability
to use) the algorithms, source code, computer programs, or textual material con-
tained in this publication. This includes, but is not limited to, loss of revenue or
profit, or other incidental, physical, or consequential damages arising out of the
use of this Work.
The sole remedy in the event of a claim of any kind is expressly limited to replace-
ment of the book and/or disc, and only at the discretion of the Publisher. The use
of “implied warranty” and certain “exclusions” vary from state to state, and might
not apply to the purchaser of this product.
(Companion files are also available for downloading from the publisher at
[email protected].)
B C L
ash ommand ine
and
s s
heLL Cripts
P P
ocket rimer
Oswald Campesato
MERCURY LEARNING AND INFORMATION
Dulles, Virginia
Boston, Massachusetts
New Delhi
Copyright © 2020 by Mercury Learning and inforMation LLC.
All rights reserved.
This publication, portions of it, or any accompanying software may not be reproduced in
any way, stored in a retrieval system of any type, or transmitted by any means, media,
electronic display or mechanical display, including, but not limited to, photocopy, record-
ing, Internet postings, or scanning, without prior permission in writing from the publisher.
Publisher: David Pallai
Mercury Learning and inforMation
22841 Quicksilver Drive
Dulles, VA 20166
[email protected]
www.merclearning.com
(800) 232-0223
O. Campesato. Bash Command Line and Shell Scripts Pocket Primer.
ISBN: 978-1-68392-504-0
The publisher recognizes and respects all marks used by companies, manufacturers, and
developers as a means to distinguish their products. All brand names and product names
mentioned in this book are trademarks or service marks of their respective companies. Any
omission or misuse (of any kind) of service marks or trademarks, etc. is not an attempt to
infringe on the property of others.
Library of Congress Control Number: 2020935567
202122321 Printed on acid-free paper in the United States of America.
Our titles are available for adoption, license, or bulk purchase by institutions,
corporations, etc. For additional information, please contact the Customer
Service Dept. at (800) 232-0223(toll free).
Digital versions of our titles are available at: www.academiccourseware.com and other
electronic vendors. Companion files are available from the publisher by writing to
[email protected].
The sole obligation of Mercury Learning and inforMation to the purchaser is to replace
the book and/or disc, based on defective materials or faulty workmanship, but not based
on the operation or functionality of the product.
I’d like to dedicate this book to my parents –
may this bring joy and happiness into their lives.
CONTENTS
Preface xv
Chapter 1: Introduction 1
What is Unix? 2
Available Shell Types 2
What is bash? 3
Getting help for Commands 4
bash
Navigating Around Directories 4
The Command 4
history
Listing Filenames with the Command 5
ls
Displaying Contents of Files 8
The Command 8
cat
The and Commands 9
head tail
The Pipe Symbol 10
The Command 11
fold
File Ownership: Owner, Group, and World 11
Hidden Files 12
Handling Problematic Filenames 13
Working with Environment Variables 13
The Command 13
env
Useful Environment Variables 14
Setting the Environment Variable 14
PATH
Specifying Aliases and Environment Variables 15
Finding Executable Files 16
The Command and the echo Command 17
printf
The Command 17
cut
viii • Bash Command Line and Shell Scripts
The echo Command and Whitespaces 18
Command Substitution (“backtick”) 20
The “pipe” Symbol and Multiple Commands 20
Using a Semicolon to Separate Commands 21
The Command 22
paste
Inserting Blank Lines with the Command 22
paste
A Simple Use Case with the Command 23
paste
A Simple Use Case with and Commands 24
cut paste
What about zsh? 25
Switching between bash and zsh 26
Configuring zsh 26
Summary 26
Chapter 2: Files and Directories 29
Create, Copy, Remove, and Move Files 29
Creating Text Files 29
Copying Files 30
Copy Files with Command Substitution 30
Deleting Files 31
Moving Files 32
The Command 32
ln
The basename, dirname, and file Commands 33
The Command 33
wc
The Command 34
cat
The Command and the Command 34
more less
The Command 35
head
The tail Command 36
Comparing File Contents 38
The Parts of a Filename 38
Working with File Permissions 39
The Command 40
chmod
Changing owner, permissions, and groups 40
The umask and ulimit Commands 41
Working with Directories 41
Absolute and Relative Directories 41
Absolute/Relative Pathnames 41
Creating Directories 42
Removing Directories 43
Navigating to Directories 43
Moving Directories 44
Using Quote Characters 44
Streams and Redirection Commands 45
Working with Metacharacters 46
Working with Character Classes 47
Contents • ix
MetaCharacters and Character Classes 48
Digits and Characters 48
Working with “^” and “\” and “!” 49
Filenames and Metacharacters 49
Summary 50
Chapter 3: Useful Commands 51
The join Command 52
The Command 52
fold
The Command 53
split
The Command 53
sort
The Command 56
uniq
How to Compare Files 56
The Command 57
od
The Command 57
tr
A Simple Use Case 60
The Command 61
find
The Command 62
tee
File Compression Commands 63
The command 63
tar
The Command 63
cpio
The gzip and gunzip Commands 64
The bunzip2 Command 64
The zip Command 65
Commands for zip Files and bz Files 65
Internal Field Separator (IFS) 65
Data From a Range of Columns in a Dataset 66
Working with Uneven Rows in Datasets 68
Summary 68
Chapter 4: Conditional Logic and Loops 71
Quick Overview of Operators in bash 71
Arithmetic Operations and Operators 72
The expr Command 72
Arithmetic Operators 73
Boolean and Numeric Operators 73
Compound Operators and Numeric Operators 74
Working with Variables 74
Assigning Values to Variables 75
The Command for User Input 76
read
Boolean Operators and String Operators 76
Compound Operators and String Operators 77
File Test Operators 78
Compound Operators and File Operators 79