Page
1
Pro Bash Learn to Script and Program the GNU/Linux Shell — Third Edition — Jayant Varma Chris F. A. Johnson
Page
2
Pro Bash Learn to Script and Program the GNU/Linux Shell Third Edition Jayant Varma Chris F. A. Johnson
Page
3
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
Page
4
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
Page
5
(This page has no text content)
Page
6
(This page has no text content)
Page
7
(This page has no text content)
Page
8
(This page has no text content)
Page
9
(This page has no text content)
Page
10
(This page has no text content)
Page
11
(This page has no text content)
Page
12
(This page has no text content)
Page
13
(This page has no text content)
Page
14
(This page has no text content)
Page
15
(This page has no text content)
Page
16
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.
Page
17
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.
Page
18
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.
Page
19
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
Page
20
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