Pro Bash Learn to Script and Program the GNU/Linux Shell — Third Edition — Jayant Varma Chris F. A. Johnson
Pro Bash Learn to Script and Program the GNU/Linux Shell Third Edition Jayant Varma Chris F. A. Johnson
Pro Bash: Learn to Script and Program the GNU/Linux Shell, Third Edition ISBN-13 (pbk): 978-1-4842-9587-8 ISBN-13 (electronic): 978-1-4842-9588-5 https://doi.org/10.1007/978-1-4842-9588-5 Copyright © 2023 by Jayant Varma, Chris F. A. Johnson This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: James Robinson-Prior Development Editor: Jim Markham Editorial Assistant: Gryffin Winkler Cover image designed by Clker-Free-Vector-Images on Pixabay (https://pixabay.com/) Distributed to the book trade worldwide by Springer Science+Business Media New York, 1 New York Plaza, 1 FDR Dr, New York, NY 10004. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer- sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail booktranslations@springernature.com; for reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub. For more detailed information, please visit https://www.apress.com/gp/services/ source-code. Paper in this product is recyclable. Jayant Varma Berwick, VIC, Australia Chris F. A. Johnson Toronto, ON, Canada
To all the people that have helped me become what I am today: My mother and father (may they rest in peace) My family and siblings My pets (may they rest in peace) My work family across the many organizations so far The team at Apress And, most importantly, the readers of this book
v About the Authors ��������������������������������������������������������������������������������������������������xvii About the Technical Reviewer ��������������������������������������������������������������������������������xix Acknowledgments ��������������������������������������������������������������������������������������������������xxi Chapter 1: Hello, World: Your First Shell Program ���������������������������������������������������� 1 What Is a Shell Script? ������������������������������������������������������������������������������������������������������������������ 1 The Hello World Code �������������������������������������������������������������������������������������������������������������������� 2 The Hello World Program File �������������������������������������������������������������������������������������������������������� 3 Naming the Script File ������������������������������������������������������������������������������������������������������������������ 3 Selecting a Directory for the Script ����������������������������������������������������������������������������������������������� 4 Creating the File and Running the Script �������������������������������������������������������������������������������������� 5 Choosing and Using a Text Editor �������������������������������������������������������������������������������������������������� 6 Building a Better “Hello, World!” ��������������������������������������������������������������������������������������������������� 7 Summary��������������������������������������������������������������������������������������������������������������������������������������� 7 Commands ������������������������������������������������������������������������������������������������������������������������������������ 8 Concepts ��������������������������������������������������������������������������������������������������������������������������������������� 8 Variables ��������������������������������������������������������������������������������������������������������������������������������������� 9 Exercises ��������������������������������������������������������������������������������������������������������������������������������������� 9 Chapter 2: Input, Output, and Throughput �������������������������������������������������������������� 11 Parameters and Variables ����������������������������������������������������������������������������������������������������������� 11 Positional Parameters ����������������������������������������������������������������������������������������������������������������� 12 Special *@#0$?_!- Parameters �������������������������������������������������������������������������������������������������� 13 Variables ������������������������������������������������������������������������������������������������������������������������������������� 13 Arguments and Options �������������������������������������������������������������������������������������������������������������� 13 Why You Should Avoid echo �������������������������������������������������������������������������������������������������������� 14 Table of Contents
vi printf: Formatting and Printing Data ������������������������������������������������������������������������������������������� 15 Escape Sequences ���������������������������������������������������������������������������������������������������������������������� 16 Format Specifiers ������������������������������������������������������������������������������������������������������������������������ 16 Width Specification ��������������������������������������������������������������������������������������������������������������������� 18 Printing to a Variable ������������������������������������������������������������������������������������������������������������������� 20 Line Continuation ������������������������������������������������������������������������������������������������������������������������ 20 Standard Input/Output Streams and Redirection ������������������������������������������������������������������������ 20 Redirection: >, >>, and < ����������������������������������������������������������������������������������������������������������� 21 Reading Input ������������������������������������������������������������������������������������������������������������������������������ 23 Pipelines ������������������������������������������������������������������������������������������������������������������������������������� 24 Command Substitution���������������������������������������������������������������������������������������������������������������� 24 Summary������������������������������������������������������������������������������������������������������������������������������������� 25 Commands ���������������������������������������������������������������������������������������������������������������������������������� 25 Concepts ������������������������������������������������������������������������������������������������������������������������������������� 25 Exercises ������������������������������������������������������������������������������������������������������������������������������������� 26 Chapter 3: Looping and Branching ������������������������������������������������������������������������� 27 Exit Status ����������������������������������������������������������������������������������������������������������������������������������� 27 Testing an Expression ����������������������������������������������������������������������������������������������������������������� 28 test, a�k�a� [ … ] �������������������������������������������������������������������������������������������������������������������������� 28 File Tests ������������������������������������������������������������������������������������������������������������������������������������� 28 Integer Tests �������������������������������������������������������������������������������������������������������������������������������� 29 String Tests ��������������������������������������������������������������������������������������������������������������������������������� 29 [[ … ]]: Evaluate an Expression �������������������������������������������������������������������������������������������������� 31 Enhancements over Test ������������������������������������������������������������������������������������������������������������� 31 (( …)): Evaluate an Arithmetic Expression ����������������������������������������������������������������������������������� 31 Conditional Execution ����������������������������������������������������������������������������������������������������������������� 32 if �������������������������������������������������������������������������������������������������������������������������������������������������� 32 Conditional Operators: && and || ������������������������������������������������������������������������������������������������� 34 case �������������������������������������������������������������������������������������������������������������������������������������������� 35 Looping ��������������������������������������������������������������������������������������������������������������������������������������� 36 Table of ConTenTs
vii while ������������������������������������������������������������������������������������������������������������������������������������������� 36 until ��������������������������������������������������������������������������������������������������������������������������������������������� 37 for ������������������������������������������������������������������������������������������������������������������������������������������������ 38 break ������������������������������������������������������������������������������������������������������������������������������������������� 38 continue �������������������������������������������������������������������������������������������������������������������������������������� 39 Summary������������������������������������������������������������������������������������������������������������������������������������� 39 Commands ���������������������������������������������������������������������������������������������������������������������������������� 40 Concepts ������������������������������������������������������������������������������������������������������������������������������������� 40 Exercises ������������������������������������������������������������������������������������������������������������������������������������� 40 Chapter 4: Command-Line Parsing and Expansion ������������������������������������������������� 41 Quoting ���������������������������������������������������������������������������������������������������������������������������������������� 42 Brace Expansion ������������������������������������������������������������������������������������������������������������������������� 44 Tilde Expansion ��������������������������������������������������������������������������������������������������������������������������� 46 Parameter and Variable Expansion ��������������������������������������������������������������������������������������������� 47 Arithmetic Expansion ������������������������������������������������������������������������������������������������������������������ 48 Command Substitution���������������������������������������������������������������������������������������������������������������� 51 Word Splitting ����������������������������������������������������������������������������������������������������������������������������� 51 Pathname Expansion ������������������������������������������������������������������������������������������������������������������ 53 Process Substitution ������������������������������������������������������������������������������������������������������������������� 54 Parsing Options ��������������������������������������������������������������������������������������������������������������������������� 55 Summary������������������������������������������������������������������������������������������������������������������������������������� 58 Commands ���������������������������������������������������������������������������������������������������������������������������������� 58 Exercises ������������������������������������������������������������������������������������������������������������������������������������� 58 Chapter 5: Parameters and Variables ��������������������������������������������������������������������� 61 The Naming of Variables ������������������������������������������������������������������������������������������������������������� 61 The Scope of a Variable: Can You See It from Here? ������������������������������������������������������������������� 62 Shell Variables ���������������������������������������������������������������������������������������������������������������������������� 65 Shell Variables ���������������������������������������������������������������������������������������������������������������������������� 65 Parameter Expansion ������������������������������������������������������������������������������������������������������������������ 67 Bourne Shell�������������������������������������������������������������������������������������������������������������������������������� 67 Table of ConTenTs
viii ${var:-default} and ${var-default}: Use Default Values ���������������������������������������������������������������� 67 ${var:+alternate} and ${var+alternate}: Use Alternate Values ���������������������������������������������������� 68 ${var:=default} and ${var=default}: Assign Default Values ��������������������������������������������������������� 70 ${var:?message} and ${var?message}: Display Error Message If Empty or Unset ���������������������� 70 POSIX Shell ��������������������������������������������������������������������������������������������������������������������������������� 71 ${#var}: Length of Variable’s Contents ���������������������������������������������������������������������������������������� 71 ${var%PATTERN}: Remove the Shortest Match from the End ����������������������������������������������������� 72 ${var%%PATTERN}: Remove the Longest Match from the End ��������������������������������������������������� 73 ${var#PATTERN}: Remove the Shortest Match from the Beginning��������������������������������������������� 73 ${var##PATTERN}: Remove the Longest Match from the Beginning ������������������������������������������� 73 bash �������������������������������������������������������������������������������������������������������������������������������������������� 74 ${var//PATTERN/STRING}: Replace All Instances of PATTERN with STRING ��������������������������������� 74 ${var:OFFSET:LENGTH}: Return a Substring of $var �������������������������������������������������������������������� 75 ${!var}: Indirect Reference ���������������������������������������������������������������������������������������������������������� 76 bash-4�0 �������������������������������������������������������������������������������������������������������������������������������������� 76 ${var^PATTERN}: Convert to Uppercase �������������������������������������������������������������������������������������� 76 ${var,PATTERN}: Convert to Lowercase ��������������������������������������������������������������������������������������� 77 Positional Parameters ����������������������������������������������������������������������������������������������������������������� 77 Arrays ������������������������������������������������������������������������������������������������������������������������������������������ 78 Integer-Indexed Arrays ���������������������������������������������������������������������������������������������������������������� 79 Displaying Arrays ������������������������������������������������������������������������������������������������������������������������ 79 Assigning Array Elements ����������������������������������������������������������������������������������������������������������� 80 Associative Arrays ����������������������������������������������������������������������������������������������������������������������� 81 Summary������������������������������������������������������������������������������������������������������������������������������������� 82 Commands ���������������������������������������������������������������������������������������������������������������������������������� 82 Concepts ������������������������������������������������������������������������������������������������������������������������������������� 83 Exercises ������������������������������������������������������������������������������������������������������������������������������������� 83 Table of ConTenTs
ix Chapter 6: Shell Functions �������������������������������������������������������������������������������������� 85 Definition Syntax ������������������������������������������������������������������������������������������������������������������������� 85 Compound Commands ���������������������������������������������������������������������������������������������������������������� 88 Getting Results ���������������������������������������������������������������������������������������������������������������������������� 89 Set Different Exit Codes �������������������������������������������������������������������������������������������������������������� 89 Print the Result ��������������������������������������������������������������������������������������������������������������������������� 90 Place Results in One or More Variables �������������������������������������������������������������������������������������� 91 Function Libraries ����������������������������������������������������������������������������������������������������������������������� 92 Using Functions from Libraries ��������������������������������������������������������������������������������������������������� 92 Sample Script ����������������������������������������������������������������������������������������������������������������������������� 93 Summary������������������������������������������������������������������������������������������������������������������������������������� 95 Commands ���������������������������������������������������������������������������������������������������������������������������������� 95 Exercises ������������������������������������������������������������������������������������������������������������������������������������� 96 Chapter 7: String Manipulation ������������������������������������������������������������������������������ 97 Concatenation ����������������������������������������������������������������������������������������������������������������������������� 98 Repeat Character to a Given Length ������������������������������������������������������������������������������������������� 99 Processing Character by Character ������������������������������������������������������������������������������������������ 100 Reversal ������������������������������������������������������������������������������������������������������������������������������������ 101 Case Conversion ����������������������������������������������������������������������������������������������������������������������� 102 Comparing Contents Without Regard to Case ��������������������������������������������������������������������������� 105 Check for Valid Variable Name �������������������������������������������������������������������������������������������������� 106 Insert One String into Another ��������������������������������������������������������������������������������������������������� 107 Examples����������������������������������������������������������������������������������������������������������������������������������� 108 Overlay �������������������������������������������������������������������������������������������������������������������������������������� 108 Examples����������������������������������������������������������������������������������������������������������������������������������� 109 Trim Unwanted Characters �������������������������������������������������������������������������������������������������������� 110 Examples����������������������������������������������������������������������������������������������������������������������������������� 111 Index ����������������������������������������������������������������������������������������������������������������������������������������� 111 Summary����������������������������������������������������������������������������������������������������������������������������������� 113 Table of ConTenTs
x Commands �������������������������������������������������������������������������������������������������������������������������������� 113 Functions ���������������������������������������������������������������������������������������������������������������������������������� 113 Exercises ����������������������������������������������������������������������������������������������������������������������������������� 114 Chapter 8: File Operations and Commands ���������������������������������������������������������� 115 Reading a File ��������������������������������������������������������������������������������������������������������������������������� 116 External Commands ������������������������������������������������������������������������������������������������������������������ 117 cat ��������������������������������������������������������������������������������������������������������������������������������������������� 118 head ������������������������������������������������������������������������������������������������������������������������������������������ 119 touch ����������������������������������������������������������������������������������������������������������������������������������������� 120 ls ����������������������������������������������������������������������������������������������������������������������������������������������� 121 cut ��������������������������������������������������������������������������������������������������������������������������������������������� 122 wc ��������������������������������������������������������������������������������������������������������������������������������������������� 123 Regular Expressions ����������������������������������������������������������������������������������������������������������������� 124 grep ������������������������������������������������������������������������������������������������������������������������������������������� 124 sed �������������������������������������������������������������������������������������������������������������������������������������������� 125 awk ������������������������������������������������������������������������������������������������������������������������������������������� 127 File Name Expansion Options ��������������������������������������������������������������������������������������������������� 130 nullglob ������������������������������������������������������������������������������������������������������������������������������������� 131 failglob �������������������������������������������������������������������������������������������������������������������������������������� 131 dotglob �������������������������������������������������������������������������������������������������������������������������������������� 132 extglob �������������������������������������������������������������������������������������������������������������������������������������� 132 ?(pattern-list) ���������������������������������������������������������������������������������������������������������������������������� 133 *(pattern-list) ���������������������������������������������������������������������������������������������������������������������������� 133 @(pattern-list) ��������������������������������������������������������������������������������������������������������������������������� 133 +(pattern-list) ��������������������������������������������������������������������������������������������������������������������������� 133 !(pattern-list) ����������������������������������������������������������������������������������������������������������������������������� 134 nocaseglob �������������������������������������������������������������������������������������������������������������������������������� 134 globstar ������������������������������������������������������������������������������������������������������������������������������������� 135 Summary����������������������������������������������������������������������������������������������������������������������������������� 136 Table of ConTenTs
xi Shell Options ����������������������������������������������������������������������������������������������������������������������������� 136 External Commands ������������������������������������������������������������������������������������������������������������������ 136 Exercises ����������������������������������������������������������������������������������������������������������������������������������� 137 Chapter 9: Reserved Words and Built-In Commands �������������������������������������������� 139 help, Display Information About Built-In Commands ���������������������������������������������������������������� 139 time, Print Time Taken for Execution of a Command ���������������������������������������������������������������� 141 read, Read a Line from an Input Stream ������������������������������������������������������������������������������������������� 142 -r, Read Backslashes Literally ��������������������������������������������������������������������������������������������� 142 -e, Get Input with the readline Library ��������������������������������������������������������������������������������� 143 -a, Read Words into an Array ����������������������������������������������������������������������������������������������� 144 -d DELIM, Read Until DELIM Instead of a Newline ��������������������������������������������������������������� 144 -n NUM, Read a Maximum of NUM Characters �������������������������������������������������������������������� 145 -s, Do Not Echo Input Coming from a Terminal �������������������������������������������������������������������� 145 -p PROMPT:, Output PROMPT Without a Trailing Newline ���������������������������������������������������� 145 -t TIMEOUT, Only Wait TIMEOUT Seconds for Complete Input ��������������������������������������������� 145 -u FD: Read from File Descriptor FD Instead of the Standard Input ������������������������������������ 146 -i TEXT, Use TEXT As the Initial Text for Readline ���������������������������������������������������������������� 147 eval, Expand Arguments and Execute the Resulting Command ������������������������������������������ 148 Poor Man’s Arrays ��������������������������������������������������������������������������������������������������������������������� 148 Setting Multiple Variables from One Command ������������������������������������������������������������������� 151 type, Display Information About Commands ������������������������������������������������������������������������ 152 builtin, Execute a Built-In Command ����������������������������������������������������������������������������������� 154 command, Execute a Command or Display Information About Commands ������������������������� 154 pwd, Print the Current Working Directory ���������������������������������������������������������������������������� 155 unalias, Remove One or More Aliases ��������������������������������������������������������������������������������� 155 Summary����������������������������������������������������������������������������������������������������������������������������������� 157 Commands and Reserved Words ����������������������������������������������������������������������������������������� 157 Deprecated Commands ������������������������������������������������������������������������������������������������������� 158 Exercise ������������������������������������������������������������������������������������������������������������������������������������ 158 Table of ConTenTs
xii Chapter 10: Writing Bug-Free Scripts and Debugging the Rest���������������������������� 159 Prevention Is Better Than Cure ������������������������������������������������������������������������������������������������� 160 Structure Your Programs ����������������������������������������������������������������������������������������������������������� 160 Comments ��������������������������������������������������������������������������������������������������������������������������������� 160 Initialization of Variables ����������������������������������������������������������������������������������������������������������� 161 Function Definitions ������������������������������������������������������������������������������������������������������������������ 162 Runtime Configuration and Options ������������������������������������������������������������������������������������������ 162 Process Information ������������������������������������������������������������������������������������������������������������������ 163 Document Your Code ����������������������������������������������������������������������������������������������������������������� 164 Format Your Code Consistently ������������������������������������������������������������������������������������������������� 164 The K�I�S�S� Principle ����������������������������������������������������������������������������������������������������������������� 165 Grouping Commands ����������������������������������������������������������������������������������������������������������������� 166 Test As You Go ��������������������������������������������������������������������������������������������������������������������������� 167 Debugging a Script ������������������������������������������������������������������������������������������������������������������� 169 Summary����������������������������������������������������������������������������������������������������������������������������������� 174 Exercises ����������������������������������������������������������������������������������������������������������������������������������� 174 Chapter 11: Programming for the Command Line ������������������������������������������������ 175 Manipulating the Directory Stack ��������������������������������������������������������������������������������������������� 175 cd ���������������������������������������������������������������������������������������������������������������������������������������������� 176 pd ���������������������������������������������������������������������������������������������������������������������������������������������� 177 cdm ������������������������������������������������������������������������������������������������������������������������������������������� 177 dirs Built-In Command �������������������������������������������������������������������������������������������������������������� 178 menu ����������������������������������������������������������������������������������������������������������������������������������������� 179 Filesystem Functions ���������������������������������������������������������������������������������������������������������������� 181 l ������������������������������������������������������������������������������������������������������������������������������������������������� 181 lsr ���������������������������������������������������������������������������������������������������������������������������������������������� 182 cp, mv ��������������������������������������������������������������������������������������������������������������������������������������� 183 md ��������������������������������������������������������������������������������������������������������������������������������������������� 184 Miscellaneous Functions ���������������������������������������������������������������������������������������������������������� 184 pr1 ��������������������������������������������������������������������������������������������������������������������������������������������� 184 Table of ConTenTs
xiii calc ������������������������������������������������������������������������������������������������������������������������������������������� 186 Managing Man Pages ��������������������������������������������������������������������������������������������������������������� 186 sman ����������������������������������������������������������������������������������������������������������������������������������������� 186 sus �������������������������������������������������������������������������������������������������������������������������������������������� 187 k ������������������������������������������������������������������������������������������������������������������������������������������������ 187 Games ��������������������������������������������������������������������������������������������������������������������������������������� 188 The fifteen Puzzle ��������������������������������������������������������������������������������������������������������������������� 189 Summary����������������������������������������������������������������������������������������������������������������������������������� 195 Exercises ����������������������������������������������������������������������������������������������������������������������������������� 195 Chapter 12: Runtime Configuration ���������������������������������������������������������������������� 197 Defining Variables ��������������������������������������������������������������������������������������������������������������������� 197 Command-Line Options and Arguments ����������������������������������������������������������������������������������� 198 Menus ��������������������������������������������������������������������������������������������������������������������������������������� 198 Q&A Dialogue ���������������������������������������������������������������������������������������������������������������������������� 199 Configuration Files �������������������������������������������������������������������������������������������������������������������� 200 Scripts with Several Names ������������������������������������������������������������������������������������������������������ 202 Environment Variables �������������������������������������������������������������������������������������������������������������� 203 All Together Now ����������������������������������������������������������������������������������������������������������������������� 204 Script Information ��������������������������������������������������������������������������������������������������������������������� 205 Default Configuration ���������������������������������������������������������������������������������������������������������������� 205 Screen Variables ����������������������������������������������������������������������������������������������������������������������� 206 Function Definitions ������������������������������������������������������������������������������������������������������������������ 207 Function: die ����������������������������������������������������������������������������������������������������������������������������� 207 Function: menu ������������������������������������������������������������������������������������������������������������������������� 207 The Upload Settings Menu �������������������������������������������������������������������������������������������������������� 208 Function: qa ������������������������������������������������������������������������������������������������������������������������������ 211 Function: print_config ��������������������������������������������������������������������������������������������������������������� 212 Function: readline ��������������������������������������������������������������������������������������������������������������������� 213 Parse Command-Line Options �������������������������������������������������������������������������������������������������� 214 Table of ConTenTs
xiv Bits and Pieces ������������������������������������������������������������������������������������������������������������������������� 215 Summary����������������������������������������������������������������������������������������������������������������������������������� 217 Exercises ����������������������������������������������������������������������������������������������������������������������������������� 217 Chapter 13: Data Processing �������������������������������������������������������������������������������� 219 Arrays ���������������������������������������������������������������������������������������������������������������������������������������� 219 Holes in an Indexed Array ��������������������������������������������������������������������������������������������������������� 219 Using an Array for Sorting ��������������������������������������������������������������������������������������������������������� 221 Insertion Sort Function �������������������������������������������������������������������������������������������������������������� 223 Searching an Array �������������������������������������������������������������������������������������������������������������������� 224 Reading an Array into Memory�������������������������������������������������������������������������������������������������� 225 Two-Dimensional Grids ������������������������������������������������������������������������������������������������������������� 228 Working with Single-String Grids ���������������������������������������������������������������������������������������������� 229 Function: initgrid ����������������������������������������������������������������������������������������������������������������������� 229 Function: gridindex ������������������������������������������������������������������������������������������������������������������� 230 Function: putgrid ����������������������������������������������������������������������������������������������������������������������� 231 Function: getgrid ����������������������������������������������������������������������������������������������������������������������� 232 Function: showgrid ������������������������������������������������������������������������������������������������������������������� 233 Function: rshowgrid ������������������������������������������������������������������������������������������������������������������ 234 Two-Dimensional Grids Using Arrays ���������������������������������������������������������������������������������������� 235 Function: initagrid ��������������������������������������������������������������������������������������������������������������������� 237 Function: putagrid ��������������������������������������������������������������������������������������������������������������������� 238 Function: getagrid ��������������������������������������������������������������������������������������������������������������������� 238 Function: showagrid ������������������������������������������������������������������������������������������������������������������ 239 Function: rshowagrid ���������������������������������������������������������������������������������������������������������������� 239 Data File Formats ���������������������������������������������������������������������������������������������������������������������� 240 Line-Based Records ������������������������������������������������������������������������������������������������������������������ 240 Delimiter-Separated Values������������������������������������������������������������������������������������������������������� 241 Fixed-Length Fields ������������������������������������������������������������������������������������������������������������������ 244 Table of ConTenTs
xv Block File Formats �������������������������������������������������������������������������������������������������������������������� 245 Summary����������������������������������������������������������������������������������������������������������������������������������� 247 Exercises ����������������������������������������������������������������������������������������������������������������������������������� 247 Chapter 14: Scripting the Screen ������������������������������������������������������������������������� 249 Teletypewriter vs� Canvas ��������������������������������������������������������������������������������������������������������� 250 Stretching the Canvas ��������������������������������������������������������������������������������������������������������������� 250 Control Sequence Introducer ���������������������������������������������������������������������������������������������������� 251 Priming the Canvas ������������������������������������������������������������������������������������������������������������������� 251 Moving the Cursor ��������������������������������������������������������������������������������������������������������������������� 252 Changing Rendition Modes and Colors ������������������������������������������������������������������������������������� 253 Placing a Block of Text on the Screen ��������������������������������������������������������������������������������������� 255 Scrolling Text ����������������������������������������������������������������������������������������������������������������������������� 259 Rolling Dice ������������������������������������������������������������������������������������������������������������������������������� 260 Summary����������������������������������������������������������������������������������������������������������������������������������� 263 Exercises ����������������������������������������������������������������������������������������������������������������������������������� 263 Chapter 15: Entry-Level Programming ����������������������������������������������������������������� 265 Single-Key Entry ����������������������������������������������������������������������������������������������������������������������� 265 Function Library, key-funcs ������������������������������������������������������������������������������������������������������ 266 History in Scripts ����������������������������������������������������������������������������������������������������������������������� 274 Sanity Checking ������������������������������������������������������������������������������������������������������������������������ 275 Form Entry �������������������������������������������������������������������������������������������������������������������������������� 276 Reading the Mouse ������������������������������������������������������������������������������������������������������������������� 278 Summary����������������������������������������������������������������������������������������������������������������������������������� 282 Exercises ����������������������������������������������������������������������������������������������������������������������������������� 283 Appendix A: Shell Variables ���������������������������������������������������������������������������������� 285 Index ��������������������������������������������������������������������������������������������������������������������� 301 Table of ConTenTs
xvii About the Authors Jayant Varma has donned many hats in the tech field. He has a richer and wider experience across several domains and industries. He has worked in several capacities from an IC (Individual Contributor) to managing multiple large teams across geographies. He has taught at a couple of Australian universities and is a speaker and has conducted multiple workshops. He has several books published by Apress on topics like SwiftUI, Lua and Xcode as well as open source topics like Linux, bash, and shell scripting. He loves to travel and explore new places. Chris F. A. Johnson was introduced to Unix in 1990 and learned shell scripting because there was no C compiler on the system. His first major project was a menu-driven, user- extensible database system with report generator. Chris is now retired and currently resides in Toronto, Canada.
xix About the Technical Reviewer Emma Saroyan graduated with a BS in computer science. She worked at startups at the intersection of technology and education, and she enjoys sharing her knowledge and learning from fellow developers. Emma is currently a developer advocate, mentor, and educator.
xxi Acknowledgments Writing a book is not an easy task especially at a time where technology changes and everything is online. Add to that the time constraints with life and work being in the way. This book is going to be in its third revision, which is a good milestone and indicates that there are people that are still reading. A book cannot publish itself though there are several articles on the use of AI and self-publishing; it takes a lot of effort from everyone involved, from the author to the publishing house to the reviewer. This book is a result of sweat and hard work and the hope that it will serve and continue to serve the readers that are interested in bash and shell programming. I have to also acknowledge my family that has seen me toiling away at the chapters after work, though their major complaint has been that they have not seen me during this process. Lastly, my passion is coding, teaching, and writing. As my career progressed, the days were consumed with more and more meetings and business/strategy discussions. The third edition came as pressure on me to juggle the task as well as a relief from those back-to-back meetings. Hope you like this revision and it has been helpful in your journey.
1 CHAPTER 1 Hello, World: Your First Shell Program A shell script is a file containing one or more commands that you would type on the command line. This chapter describes how to create such a file and make it executable. It also covers some other issues surrounding shell scripts, including what to name the files, where to put them, and how to run them. What Is a Shell Script? A shell script is a file that contains one or more commands, which can be commands that you might type on a command line. The script file when executed (run) would run these commands one after the other like how one might have typed them on a command line one after the other sequentially. In this chapter, we shall look at how to create such a file and make it executable. We shall also cover some other points like naming these script files, where to save them, and how to run them. When starting with a new language, the first program that a developer writes is a standard HelloWorld program, one that prints “Hello, World!” to the terminal. This simple program demonstrates quite a few important concepts. The code itself is the simplest part of this chapter. The file naming and deciding where to place the files are not complicated either but are important. For a major portion of the book, we shall work with the terminal; it could be a virtual terminal, a terminal window, or even a dumb terminal. For many of the readers, some of these might be a new concept. For simplicity’s sake, we shall use the term “terminal” to represent them across all the platforms and across all types. In the terminal, the shell will immediately execute any commands that are typed (after pressing Enter, of course). © Jayant Varma, Chris F. A. Johnson 2023 J. Varma and C. F. A. Johnson, Pro Bash, https://doi.org/10.1007/978-1-4842-9588-5_1
2 When you start a terminal, you can first type the following command to find your home directory: $ echo "$HOME" /home/jayant This prints the home directory, which is stored in the variable called HOME; when using a variable, we use the $ sign prepended to the variable name, in this case $HOME. We can also find the current directory using the pwd (print current/working directory) command or the PWD variable as $ pwd /home/jayant $ echo "$PWD" /home/jayant When we start a terminal, we are generally in the home directory; however, if at any time we are not in the home directory, we can switch back to the home directory by using the cd command. cd stands for Change Directory. When used without any parameters, it will take us back to the home directory. When used with parameters, it will change to the directory name as passed in the parameters (if such a directory existed in the path). We can also use the .. as parameters to go back one level on the directory hierarchy (if we are not in the parent directory already). The Hello World Code The code is as simple as $ echo Hello, World! Hello, World! There are three words on this command line: the command itself and two arguments. The command, echo, prints its arguments separated by a single space and terminated with a newline. An important note here is that the text after echo is parameters and these parameters are separated by spaces, so we have Hello, as the first parameter and World! as the second. Chapter 1 hello, World: Your First shell program
Comments 0
Loading comments...
Reply to Comment
Edit Comment