bash Idioms
by Carl Albing and JP Vossen
Copyright 2022 Carl Albing and JP Vossen. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Acquisitions Editor: Suzanne McQuade
- Development Editor: Nicole Tache
- Production Editor: Kristen Brown
- Copyeditor: TK
- Proofreader: TK
- Indexer: TK
- Interior Designer: David Futato
- Cover Designer: TK
- Illustrator: TK
- April 2022 Year: First Edition
Revision History for the First Edition
- 2021-09-08: First Release
- 2021-11-03: Second Release
- 2022-01-11: Third Release
See http://oreilly.com/catalog/errata.csp?isbn=9781492094753 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. bash Idioms, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-492-09475-3
[TK]
Preface
Websters Dictionary defines idiom as:
1 : an expression in the usage of a language that is peculiar to itself either in having a meaning that cannot be derived from the conjoined meanings of its elements (such as up in the air for undecided) or in its grammatically atypical use of words (such as give way)
2a : the language peculiar to a people or to a district, community, or class : dialect
2b : the syntactical, grammatical, or structural form peculiar to a language
3 : a style or form of artistic expression that is characteristic of an individual, a period or movement, ora medium or instrument
Why bash Idioms? One word, readability. Or perhaps a different word, understandability. In this book those words mean the same thing. We dont have to explain why thats critically important, unless this is the first book about programming you are reading , you already get it. Readability means being able to read and understand code, especially code that someone else wrote, but it also means being able to write code that you, or someone else, can later read and understand. Clearly these aspects are different sides of the same coin, so well explore both the clear idioms to use and decode the obscure ones not to.
We think of bash, informally, as a language to use to run things. If you need to do a lot of heavy data processing, bash may not be the first choice. You can do it, but it might not be pretty. Of course, if you already have the data processing tools you need and you just have to glue them together, well then bash is great. But if all we do is run things, why do we care about the idioms of the language or its structural form? Programs grow, features creep, things change and there is nothing more permanent than a temporary solution. Sooner or later someone is going to have to read the code, understand it, and make changes. If its written using an obscure idiom, the job is that much harder.
In a lot of ways bash doesnt look like other languages. It has a lot of history (some may say baggage) and there are reasons it looks and works the way it does. Were not going to talk about that very much, because we cover a lot of that in the bash Cookbook. Shell scripts arguably run the world, at least in the Unix and Linux worlds (and Linux pretty much runs the cloud world), and a huge majority of those scripts are interpreted by bash
. Maintaining backwards compatibility back to the very first Unix shells is critically important, but it imposes somebaggage.
Then theres the dialects. The big one, especially for backwards compatibility, is POSIX . We wont talk too much about that either, after all this is bash Idioms, not POSIX Idioms. Other dialects may appear when programmers write bash code in a style that is more characteristic of another language they know. A flow that makes sense in C may feel clumsy or disjointed in bash though. So with this book, we intend to demonstrate a style or form of expression that is characteristic of bash. Python programmers talk about their style as pythonic. Wed like this book to illustrate and illuminate code that is bashy.
By the end of this book the reader will be able to:
Write useful, flexible, and readable bash codewith style
Decode bash code such as ${MAKEMELC,,}
and ${PATHNAME##*/}
Save time and ensure consistency when automating tasks
Amaze and impress colleagues with bash idioms
Discover how bash idioms can make your code clean and concise
Running bash
Were going to assume you are already programming in bash, and therefor you dont need to learn where to find or install it. Of course bash
is just there in almost all Linux distributions, and already there by default or installable on virtually any other operating system. You can get it in Windows using Git for Windows , or various other options we cover in the bash Cookbook.
bash on Mac
That said, watch out for stock bash
on a Mac, its quite old and many newer idioms (v4+) wont work. You can obtain a newer version by installing MacPorts, Homebrew, or Fink and looking for bash. According to https://discussions.apple.com/thread/250722978 the issue is that newer versions of bash use GPLv3 which is a problem for Apple.
On a related note, https://support.apple.com/en-us/HT208050 says that macOS Catalina and newer will use zsh
as the default login and interactive shell. Zsh is mostly compatible with bash, but some code in this book wont work unmodified. Bash isnt going away on Macs (yet, at least) and having Zsh as your default shell will not affect a bash shebang line, but again, unless you upgrade, youll be stuck with stone-age bash.
Weve tagged example scripts Does not work on zsh 5.4.2 as a best effort clue for Mac users, but this is a book about bash so were going to stay focused.
bash in Containers
Be careful in Docker or other containers where /bin/sh
is not bash and /bin/bash
may not even exist! This applies to Internet-of-Things and other constrained environments such as industrial controllers.
/bin/sh
may be bash in POSIX mode, but it may also be Ash or Dash (https://en.wikipedia.org/wiki/Almquist_shell), or BusyBox (which is probably actually Dash), or maybe even something else. Youll need to be specific, and possibly either ensure that bash is actually installed, or stick to POSIX and avoid bashisms.