Page
1
® A P R A C T I C A L U S E R ’ S G U I D E T O W O R K I N G S M A R T E R O N T H E C O M M A N D L I N E D A V E K E R R E F F E C T IV E S H E L L K E R R E F F E C T I V E S H E L L
Page
2
(This page has no text content)
Page
4
(This page has no text content)
Page
5
® San Francisco E F F E C T I V E S H E L L A P r a c t i c a l U s e r ’s G u i d e t o Wo r k i n g S m a r t e r o n t h e C o m m a n d L i n e by Dave Kerr
Page
6
EFFECTIVE SHELL. Copyright © 2025 by Dave Kerr. This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States (CC BY-NC-SA 3.0 US) license. To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/3.0/ us/deed.en or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. Some rights reserved. When attributing this work, you must credit the author as follows: “Dave Kerr, published by No Starch Press® Inc.,” provide a link to the license, and indicate if changes were made. You may not use the material for commercial purposes. For ShareAlike purposes, if you transform or build upon the material, you must distribute your contributions under the same license as the original. Translations of this work are not covered under this license; all translation rights are reserved by the publisher. For permission to translate this work, please contact rights@nostarch.com. Moral rights of the author have been asserted. First printing 29 28 27 26 25 1 2 3 4 5 ISBN-13: 978-1-7185-0414-1 (print) ISBN-13: 978-1-7185-0415-8 (ebook) Published by No Starch Press®, Inc. 245 8th Street, San Francisco, CA 94103 phone: +1.415.863.9900 www.nostarch.com; info@nostarch.com Publisher: William Pollock Managing Editor: Jill Franklin Production Manager: Sabrina Plomitallo-González Production Editor: Jennifer Kepler Developmental Editor: Rachel Monaghan Cover Design: Gina Redman Interior Design: Octopod Studios Technical Reviewer: Mitch Frazier Copyeditors: James Brook and Rachel Monaghan Proofreader: Daniel Wolff Indexer: BIM Creatives, LLC Library of Congress Control Number: 2024048644 For permissions beyond the scope of this license or customer service inquiries, please contact info@nostarch.com. For information on distribution, bulk sales, or corporate sales: sales@nostarch.com. To report counterfeit copies or piracy: counterfeit@nostarch.com. The authorized representative in the EU for product safety and compliance is EU Compliance Partner, Pärnu mnt. 139b-14, 11317 Tallinn, Estonia, hello@eucompliancepartner.com, +3375690241. No Starch Press and the No Starch Press iron 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. [E] ®
Page
7
For Mum and Dad, who always got me books
Page
8
(This page has no text content)
Page
9
About the Author Dave Kerr is a technology consultant and software engineer who helps teams design and launch new products, scale high-performance systems, and modernize complex platforms. Passionate about automation, site reliability engineering, and developer experience, he loves coding, mentoring, pub- lishing, contributing to open source, and writing. When he’s not in the shell, he’s in the mountains or rock climbing. About the Technical Reviewer Mitch Frazier is a programmer who works for Emerson Electric doing mostly embedded systems programming in C. He also occasionally writes code in Golang, Python, Tcl, JavaScript, and bash. He previously worked for Linux Journal, both as a technical editor and as a system administrator.
Page
10
(This page has no text content)
Page
11
B R I E F C O N T E N T S Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii PART I: CORE SKILLS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Chapter 1: Flying on the Command Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Chapter 2: Thinking in Pipelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Chapter 3: Finding Files and Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 PART II: MANIPULATING TEXT AND STREAMS . . . . . . . . . . . . . . . . . . . . .45 Chapter 4: Regular Expression Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Chapter 5: Getting to Grips with grep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Chapter 6: Slicing and Dicing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Chapter 7: Advanced Text Manipulation with sed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Chapter 8: Building Commands on the Fly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 PART III: SHELL SCRIPTING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .107 Chapter 9: Shell Script Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Chapter 10: Using Variables to Store, Read, and Manipulate Data . . . . . . . . . . . . . . . . . 121 Chapter 11: Mastering Conditional Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Chapter 12: Using Loops with Files and Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Chapter 13: Functions, Parameters, and Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . 173 Chapter 14: Useful Patterns for Shell Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Page
12
x Brief Contents PART IV: BUILDING YOUR TOOLKIT . . . . . . . . . . . . . . . . . . . . . . . . . . . .205 Chapter 15: Configuring Your Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Chapter 16: Customizing Your Command Prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Chapter 17: Managing Your Dot Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 Chapter 18: Controlling Changes with Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Chapter 19: Managing Remote Git Repositories and Sharing Dot Files . . . . . . . . . . . . . . . 275 PART V: ADVANCED TECHNIQUES . . . . . . . . . . . . . . . . . . . . . . . . . . . .295 Chapter 20: Shell Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 Chapter 21: Alternatives to Shell Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 Chapter 22: The Secure Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 Chapter 23: The Power of Terminal Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 Chapter 24: Mastering the Multiplexer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 Afterword: Generative AI and the Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377 Appendix A: Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381 Appendix B: Shell Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Page
13
C O N T E N T S I N D E T A I L ACKNOWLEDGMENTS xxi INTRODUCTION xxiii What Is the Shell? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiv Who Should Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv What’s in This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv How to Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxix Bugs and Suggestions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxx Sample Files and Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxx PART I: CORE SKILLS 1 1 FLYING ON THE COMMAND LINE 3 Basic Navigation Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Go to the Beginning or End of a Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Move Back or Forward One Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Delete a Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Delete a Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Undo a Change . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Search Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Editing in Place . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Other Useful Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Clear the Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 View Your Command History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Show All Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Transpose Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2 THINKING IN PIPELINES 13 The IPO Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 stdin, stdout, and stderr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Pipelines in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Standard Input Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Standard Output Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Standard Error Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Redirection with Both stdout and stderr . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 The T-Pipe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Pipelines and the Unix Philosophy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Page
14
xii Contents in Detail 3 FINDING FILES AND FOLDERS 31 Searching with the find Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Refining a Search with find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Searching for Only Files or Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Searching by File or Folder Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Searching by Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Specifying More Than One Search Option . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Running Case-Insensitive Searches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Grouping Parts of an Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Excluding Search Results with the NOT Operator . . . . . . . . . . . . . . . . . . . . . 38 Acting on Search Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Printing Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Deleting Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Executing Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Confirming Commands Before Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Handling Symbolic Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Going Further with find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 PART II: MANIPULATING TEXT AND STREAMS 45 4 REGULAR EXPRESSION ESSENTIALS 47 Regexes in a Nutshell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Building Regexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Regex Engines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Quantifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Character Sets and Metacharacters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Anchors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Capture Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Lazy and Greedy Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Advanced Regex Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Backtracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Lookarounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Atomic Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 5 GETTING TO GRIPS WITH GREP 61 What Exactly Is grep? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Searching Through Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Using grep with Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Advanced grep Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Making a Search Case-Insensitive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Getting Additional Context for Search Results . . . . . . . . . . . . . . . . . . . . . . . . 66 Searching Through Multiple Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Filtering and Piping grep Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Combining grep with other Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Page
15
Contents in Detail xiii Alternatives to grep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 6 SLICING AND DICING TEXT 71 Extracting Heads and Tails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Replacing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Cutting Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Reversing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Sorting Text and Removing Duplicate Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Paging Through Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 7 ADVANCED TEXT MANIPULATION WITH SED 83 Transformations with sed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Replacing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Applying Multiple Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Stripping Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Appending Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Prepending Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Extracting Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Advanced Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Restructuring Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Creating Template Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Editing in Place . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Alternatives to sed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 8 BUILDING COMMANDS ON THE FLY 99 Introducing xargs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Handling Whitespace, Special Characters, and Tracing . . . . . . . . . . . . . . . . . . . . . . 101 Customizing How xargs Processes Input Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Organizing the Parameters for Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Running Commands Interactively . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Running a Command for Each Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 PART III: SHELL SCRIPTING 107 9 SHELL SCRIPT FUNDAMENTALS 109 Why Shell Scripts? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Creating a Basic Shell Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Adding Code Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Adding and Formatting Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Pipelining Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Page
16
xiv Contents in Detail Making Shell Scripts Executable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Specifying What Program Should Run a Script . . . . . . . . . . . . . . . . . . . . . . 116 Sourcing Shell Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Installing Scripts Locally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 10 USING VARIABLES TO STORE, READ, AND MANIPULATE DATA 121 Understanding Variable Scope: Environment vs . Shell Variables . . . . . . . . . . . . . . . . . 122 Exporting Shell Variables as Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . 123 Variable Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Quoting Variables and Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Using Braces to Reference Variables Explicitly . . . . . . . . . . . . . . . . . . . . . . . 127 Common Variable Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Storing a Command’s Output in a Variable . . . . . . . . . . . . . . . . . . . . . . . . 127 Managing Multiple Values with Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Storing Complex Data with Associative Arrays . . . . . . . . . . . . . . . . . . . . . . 130 Expanding Shell Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Reading and Storing User Input in Variables . . . . . . . . . . . . . . . . . . . . . . . . 132 Performing Arithmetic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Enhancing the common Command with Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 11 MASTERING CONDITIONAL LOGIC 141 The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 The test Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Using test Operators with Expressions and Files . . . . . . . . . . . . . . . . . . . . . 143 Checking Multiple test Conditions Simultaneously . . . . . . . . . . . . . . . . . . . . 145 Combining Statements on a Single Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 The else Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 The elif Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 The case Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Conditional Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Chaining Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Extending the common Command to Handle Different Shells . . . . . . . . . . . . . . . . . . . 152 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 12 USING LOOPS WITH FILES AND FOLDERS 155 The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Looping Through Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Splitting Loop Input into Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Looping Through Files and Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 Looping Through find Command Results . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Iterating with C-Style Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Looping over Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 The while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Looping Through the Lines in a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Looping Forever . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Page
17
Contents in Detail xv The until Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 The continue and break Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Creating Compact Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 Updating the common Command to Loop Through Results . . . . . . . . . . . . . . . . . . . . . 171 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 13 FUNCTIONS, PARAMETERS, AND ERROR HANDLING 173 Creating a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Variables in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Variable Scoping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Passing Parameters to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Using Array Operators in Parameter Variables . . . . . . . . . . . . . . . . . . . . . . 179 Shifting Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Function Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 Writing Results to Standard Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Avoiding Pitfalls with Command Output . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Returning Status Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Simplifying the common Command with Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 14 USEFUL PATTERNS FOR SHELL SCRIPTS 189 Ensuring Exit on Failure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Debugging Shell Scripts with the Trace Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 Checking for Existing Variables or Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 Unsetting Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Trapping Signals and Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Processing Complex Script Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Adding Syntax Highlighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Checking the Operating System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Checking for Installed Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Showing a Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Running Commands in Subshells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Anti-patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Omitting Shebangs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 Configuring Options in Shebangs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 Using Complex Logic in Shell Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 PART IV: BUILDING YOUR TOOLKIT 205 15 CONFIGURING YOUR SHELL 207 Interactive Shells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 The Default Shell Startup File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Common Startup File Customizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Page
18
xvi Contents in Detail Non-interactive Shells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Understanding Shell Script Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Loading a Startup File with BASH_ENV . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Login Shells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Checking Whether You’re in a Login Shell . . . . . . . . . . . . . . . . . . . . . . . . . 217 Loading the Shell Startup Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Changing Your Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 16 CUSTOMIZING YOUR COMMAND PROMPT 221 The Command Prompt Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 The Prompt String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Adding Color and Text Formatting to Your Prompt . . . . . . . . . . . . . . . . . . . . . . . . . . 225 Adding Data to the Command Prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 Additional Shell Prompt Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 PS2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 PS3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 PS4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 PROMPT_DIRTRIM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 PROMPT_COMMAND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 Writing a Shell Script to Customize the Command Prompt . . . . . . . . . . . . . . . . . . . . . 231 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 17 MANAGING YOUR DOT FILES 237 Dot Files Defined . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 The Default Shell Dot File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Creating Your Own Dot Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Creating the Dot Files Folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Creating shell .sh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Adding Custom Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 Testing the Shell Dot File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Sourcing the Shell Dot File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Sourcing Files from a Folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 A Dot File Installation Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 18 CONTROLLING CHANGES WITH GIT 249 What Is Git? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Creating a Git Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Adding and Resetting Changes to the Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 Committing Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 Working with Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 Creating Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 Performing Fast-Forward Merges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 Performing Recursive Merges and Handling Diverged Branches . . . . . . . . . . 261 The Git Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Page
19
Contents in Detail xvii Resolving Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 Managing Files in Your Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Deleting Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Restoring and Renaming Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 Restoring Your Working Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 Git Command Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 19 MANAGING REMOTE GIT REPOSITORIES AND SHARING DOT FILES 275 Getting Started with GitHub . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 Creating a Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 Pushing Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 Fetching Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 Pulling Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 Sharing Your Dot Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 Collaborating with Other Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 Forking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 Making Pull Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 Writing a Shell Function to Open a Pull Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 Showing Git Information in the Command Prompt . . . . . . . . . . . . . . . . . . . . . . . . . . 290 Diving Deeper into Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 Git at a Glance: A Recap of Key Concepts and Commands . . . . . . . . . . . . . . . . . . . 293 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 PART V: ADVANCED TECHNIQUES 295 20 SHELL EXPANSION 297 Shell Expansion Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 Brace Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 Tilde Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 Parameter Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 Command Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 Arithmetic Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 Word Splitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 Pathname Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 21 ALTERNATIVES TO SHELL SCRIPTING 313 When to Avoid Shell Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 Choosing a Programming Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 Characteristics of Shell-Friendly Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 Writing a Dictionary Lookup Tool in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 Defining the Tool’s Basic Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 Downloading the Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 Formatting the Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Page
20
xviii Contents in Detail Installing the Lookup Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 Improving the Lookup Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 22 THE SECURE SHELL 327 What Is SSH? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 Creating a Key Pair . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328 Setting Up an AWS Account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 Creating a Virtual Machine on AWS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 Choose an Amazon Machine Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Choose an Instance Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Review the Instance Launch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Using SSH to Connect to a Virtual Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 Dealing with Key Permission Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 Configuring SSH Hosts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 Running SSH Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 Handling Disconnections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 Transferring Files with scp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 23 THE POWER OF TERMINAL EDITORS 343 Why Use a Terminal Editor? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 Getting Started with Vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 Building a Cheatsheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 Creating a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 Navigating Through Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 Adding a Command Count . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352 Inserting Text at Specific Positions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352 Operating on a Range of Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354 Searching for Text Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 Editing Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358 Updating and Styling Your Vim Cheatsheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 Going Further with Vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360 Vimtutor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 Vimcasts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 Practical Vim and Modern Vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 24 MASTERING THE MULTIPLEXER 363 Benefits of Using a Multiplexer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365 Installing tmux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365 Window Management with tmux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367 Creating and Moving Between Panes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367 Zooming Panes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367 Creating and Moving Between Windows . . . . . . . . . . . . . . . . . . . . . . . . . . 367 Session Management with tmux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368 Starting a New Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368 Attaching and Detaching from Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . 368