bash Idioms

Book description

Shell scripts are everywhere, especially those written in bash-compatible syntax. But these scripts can be complex and obscure. Complexity is the enemy of security, but it's also the enemy of readability and understanding. With this practical book, you'll learn how to decipher old bash code and write new code that's as clear and readable as possible.

Authors Carl Albing and JP Vossen show you how to use the power and flexibility of the shell to your advantage. You may know enough bash to get by, but this book will take your skills from manageable to magnificent. Whether you use Linux, Unix, Windows, or a Mac, you'll learn how to read and write scripts like an expert. Your future you will thank you.

You'll explore the clear idioms to use and obscure ones to avoid, so that you can:

  • Write useful, flexible, and readable bash code with style
  • Decode bash code such as ${MAKEMELC,,} and ${PATHNAME##*/}
  • Save time and ensure consistency when automating tasks
  • Discover how bash idioms can make your code clean and concise

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Running bash
      1. bash on Mac
      2. bash in Containers
    2. Revision Control
    3. Hello World
    4. Conventions Used in This Book
    5. Using Code Examples
    6. O’Reilly Online Learning
    7. How to Contact Us
    8. Acknowledgments
      1. bash
      2. Reviewers
      3. O’Reilly
      4. From Carl
      5. From JP
  2. 1. A Big “if” Idiom
    1. The Big “if”
    2. Or ELSE…
    3. More than One
    4. More than One Again
    5. Don’t Do This
    6. Style and Readability: Recap
  3. 2. Looping Lingo
    1. Looping Constructs
    2. Explicit Values
    3. Similar to Python
    4. Quotes and Spaces
    5. Developing and Testing for Loops
    6. while and until
    7. Style and Readability: Recap
  4. 3. Just in CASE
    1. Make Your Case
    2. A Realistic Use Case
      1. Motivation
      2. Our Script
    3. Wrapper Scripts
    4. One More Twist
    5. Style and Readability: Recap
  5. 4. Variable Vernacular
    1. Variable Reference
    2. Parameter Expansion
      1. Shorthand for basename
      2. Path or Prefix Removal
      3. Shorthand for dirname or Suffix Removal
      4. Other Modifiers
    3. Conditional Substitutions
      1. Default Values
      2. Comma-Separated Lists
      3. Modified Value
    4. $RANDOM
    5. Command Substitution
    6. Style and Readability: Recap
  6. 5. Expressions and Arithmetic
    1. Arithmetic
      1. No Parentheses Needed
    2. Compound Commands
    3. Style and Readability: Recap
  7. 6. Functional Framework
    1. Calling Functions
    2. Defining Functions
      1. Function Parameters
      2. Function Return Values
      3. Local Variables
      4. Function Special Cases
    3. Time for printf
      1. POSIX Output
      2. Getting or Using the Date and Time
      3. printf for Reuse or Debugging
    4. Style and Readability: Recap
  8. 7. List and Hash Handling
    1. Commonalities
    2. Lists
    3. Hashes
    4. A Simple Word Count Example
    5. Style and Readability: Recap
  9. 8. Arguing
    1. Your First Argument
    2. Having Options
      1. Parsing Options
    3. Long Arguments
    4. HELP!
    5. Debug and Verbose
    6. Version
    7. Style and Readability: Recap
  10. 9. Files and More
    1. Reading Files
      1. read
      2. mapfile
      3. Brute Force
    2. Fiddling with $IFS for Fun and Profit, to Read Files
    3. Pretend Files
    4. Drop-in Directories
    5. Using a Library
    6. Shebang!
    7. Unofficial bash Strict Mode
    8. Exit Codes
    9. It’s a Trap!
    10. Here-Documents and Here-Strings
    11. Are We Interactive?
    12. Summary
  11. 10. Beyond Idioms: Working with bash
    1. Prompts
      1. bash Time Zone
    2. Getting User Input
      1. read
      2. pause
      3. select
    3. Aliases
    4. Functions
    5. Local Variables
    6. Readline Hacks
    7. Using logger from bash
    8. Handling JSON with jq
    9. Grepping the Process List
    10. Rotating Old Files
    11. Embedded Documentation
    12. bash Debugging
    13. bash Unit Testing
    14. Summary
  12. 11. Developing Your Style Guide
    1. Readability
    2. Comments
    3. Names
    4. Functions
    5. Quoting
    6. Layout
    7. Syntax
    8. Other
    9. Script Template
    10. Other Style Guides
    11. bash Linter
    12. Summary
  13. A. The bash Idioms Style Guide
    1. Readability
    2. Comments
    3. Names
    4. Functions
    5. Quoting
    6. Layout
    7. Syntax
    8. Other
    9. Script Template
  14. Index
  15. About the Authors

Product information

  • Title: bash Idioms
  • Author(s): Carl Albing, JP Vossen
  • Release date: March 2022
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492094753