Bash Quick Start Guide
Get up and running with shell scripting with Bash
Tom Ryder
BIRMINGHAM - MUMBAI
Bash Quick Start Guide
Copyright 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Gebin George
Acquisition Editor: Noyonika Das
Content Development Editor: Mohammed Yusuf Imaratwale
Technical Editor: Shweta Jadhav
Copy Editor: Safis Editing
Project Coordinator: Hardik Bhinde
Proofreader: Safis Editing
Indexer: Pratik Shirodkar
Graphics: Jason Monteiro
Production Coordinator: Nilesh Mohite
First published: September 2018
Production reference: 1270918
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78953-883-0
www.packtpub.com
For Chantelle
Tom Ryder
mapt.io
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Why subscribe?
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
Packt.com
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at customercare@packtpub.com for more details.
At www.packt.com , you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Contributors
About the author
Tom Ryder is a systems administrator living in New Zealand who works for an internet services provider. He loves terminals, text editors, network monitoring and security, Unix and GNU/Linux, shell script, and programming in general. He is also the author of the Nagios Core Administration Cookbook .
Thanks go to the Packt editors and reviewers; to Richard Stallman, Brian Fox, and Chet Ramey for the GNU Project and the Bourne-Again Shell; to the authors of Greg's Wiki and the Bash Hackers Wiki; to Cory Sadowski for many entertaining chats about shell script over the years; to my boss, James Watts, for contractual permission to write yet another book; and, of course, to the lovely Chantelle Potroz.
About the reviewers
Fabio Alessandro Locati commonly known as Fale is the director at Otelia, a public speaker, author, and open source contributor. His primary areas of expertise are Linux, automation, security, and cloud technologies. Fale has more than 14 years' experience working in IT, with many of them spent consulting for companies, including dozens of Fortune 500 companies. Fale has written Learning Ansible 2, OpenStack Cloud Security, and has been part of the review process for multiple books.
Janos Gyerik is a passionate software developer. He enjoys building software whatever the programming language, both at work and in his spare time. He helps fellow programmers by answering questions on Stack Overflow, or by giving feedback on their code on Code Review Stack Exchange. Janos makes Android apps such as Programming and Bash Quiz, and websites such as Bash One-Liners, and is always up to some geeky projects.
Cory Sadowski is an IT professional living in the United States, specializing in Perl and shell programming, GNU/Linux, and, despite best efforts, Windows administration. He loves most things GNU/Linux, video games, and other such sophisticated topics.
Packt is searching for authors like you
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Preface
The GNU Bourne-Again Shell, or Bash, is the best-known Bourne-style shell in the world, and is installed or available for installation on a huge variety of Unix-like systems. Even professionals who don't do a lot of work with Unix or Linux will need to use the Bash shell occasionally.
Bash is a language of contradictions; while it's the best-known and most widely-deployed shell of its kind, it's perhaps also one of the least-understood tools, with a terse syntax that's relatively unique among modern programming languages and can seem bizarre even to experienced users. Bash is powerful in some ways, and very limited in others. It's clear, elegant, and expressive in some ways, and terse, clumsy, and bewildering in others.
Because it's so powerful and yet so complex, and because so many computer professionals can't avoid using it at least occasionally, Bash is often learned by way of a kind of "tradition;" demonstrations by experienced administrators, reading others' scripts, copying and pasting, and asking questions and reading answers on the internet. This leads to a lot of "cargo-cult programming," and a lot of bad practices that make things unnecessarily confusing at best, and downright dangerous at worst. The available documentation for Bash is often unhelpful in addressing this problemit often teaches the same bad practices, and even when it's correct, as the official Bash manual page is, it's often too complicated and assumes too much knowledge for new users to understand it.
To avoid all that, we'll start learning good Bash from first principles, and focus almost exclusively on writing the language
Next page