O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Bash Quick Start Guide

Book Description

Learn how to write shell script effectively with Bash, to quickly and easily write powerful scripts to manage processes, automate tasks, and to redirect and filter program input and output in useful and novel ways.

Key Features

  • Demystify the Bash command line
  • Write shell scripts safely and effectively
  • Speed up and automate your daily work

Book Description

Bash and shell script programming is central to using Linux, but it has many peculiar properties that are hard to understand and unfamiliar to many programmers, with a lot of misleading and even risky information online. Bash Quick Start Guide tackles these problems head on, and shows you the best practices of shell script programming.

This book teaches effective shell script programming with Bash, and is ideal for people who may have used its command line but never really learned it in depth. This book will show you how even simple programming constructs in the shell can speed up and automate any kind of daily command-line work.

For people who need to use the command line regularly in their daily work, this book provides practical advice for using the command-line shell beyond merely typing or copy-pasting commands into the shell. Readers will learn techniques suitable for automating processes and controlling processes, on both servers and workstations, whether for single command lines or long and complex scripts. The book even includes information on configuring your own shell environment to suit your workflow, and provides a running start for interpreting Bash scripts written by others.

What you will learn

  • Understand where the Bash shell fits in the system administration and programming worlds
  • Use the interactive Bash command line effectively
  • Get to grips with the structure of a Bash command line
  • Master pattern-matching and transforming text with Bash
  • Filter and redirect program input and output
  • Write shell scripts safely and effectively

Who this book is for

People who use the command line on Unix and Linux servers already, but don't write primarily in Bash. This book is ideal for people who've been using a scripting language such as Python, JavaScript or PHP, and would like to understand and use Bash more effectively.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Bash Quick Start Guide
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. Packt.com
  5. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. What is Bash?
    1. What Bash is and is not
    2. Getting Bash
    3. Checking Bash is running
      1. Switching the login shell to Bash
    4. Identifying the Bash version number
    5. Upgrading Bash on macOS X
    6. Understanding Bash features
      1. POSIX shell script features
      2. Bash-specific features
      3. Do I need Bash?
    7. Choosing when to apply Bash
    8. Choosing when to avoid Bash
    9. Getting help with Bash
    10. Summary
  8. Bash Command Structure
    1. Using Bash interactively
    2. Interactive key bindings
    3. Simple commands
    4. Shell metacharacters
    5. Quoting
      1. Escaping
      2. Single quotes
      3. Double quotes
      4. Quote concatenation
    6. Running commands in sequence
    7. Exit values
    8. Stopping a command list on error
    9. Running a command in the background
    10. Summary
  9. Essential Commands
    1. Distinguishing command types
    2. Essential Bash builtin commands
      1. The type command
      2. The echo command
      3. The printf command
      4. The pwd command
        1. Tilde paths
      5. The cd command
        1. Changing the directory in scripts
      6. The set command
      7. The declare command
      8. The test, [, and [[ commands
    3. Essential system commands
      1. The ls command
        1. Getting filename lists without ls
      2. The mv command
      3. The cp command
      4. The rm and rmdir commands
      5. The grep command
      6. The cut command
      7. The wc command
        1. Getting file sizes with wc or du
      8. The find command
        1. Executing commands for each result
        2. A note about find and xargs
      9. The sort and uniq commands
    4. Summary
  10. Input, Output, and Redirection
    1. Redirecting output
      1. Redirection paths
      2. Avoiding overwrites
      3. Appending to files
      4. Understanding created file permissions
      5. Choosing permissions for created files
    2. Redirecting errors
      1. Combining errors with output
      2. Blocking errors completely
      3. Sending output to more than one place
    3. Redirecting input
      1. Using a long string as input with here-documents
    4. Using pipes
      1. Adding file contents to a stream
      2. Piping output from multiple programs
      3. Filtering programs
        1. The sed stream editor
        2. The AWK programming language
    5. Summary
  11. Variables and Patterns
    1. Using variables
      1. Listing variables
      2. Naming variables
        1. Variable name case
      3. Clearing variables
      4. Environment variables
        1. Calling programs with environment variables
      5. Expanding variables
      6. Reading a value into a variable
      7. Getting command output in variables
      8. Parameter expansion forms
        1. Specifying default values
        2. String chopping
        3. Extracting substrings
        4. Getting string length
        5. Substituting strings
        6. Changing case
        7. Combining parameter expansion forms
      9. Doing math in Bash
        1. Fixed or floating-point arithmetic
    2. Using globs
      1. Configuring globbing behavior
        1. Including dot files, but excluding dot and dot-dot
        2. Expanding to nothing
        3. Case-insensitive globbing
        4. Extended globbing
    3. Using arrays
      1. Glob expansion in arrays
      2. Associative arrays
    4. Summary
  12. Loops and Conditionals
    1. Using the if keyword
      1. Using the test command
      2. Using the [ command
      3. Using the [[ keyword
      4. Arithmetic conditions
    2. Switching with the case keyword
    3. Looping over shell words with for
      1. Skipping an iteration
      2. Ending the loop
      3. Misuse of for loops
    4. Using Bash's C-style for loops
    5. Using while loops
      1. Infinite loops
      2. Reading data line by line
      3. Field splitting
      4. Saving fields into arrays
      5. Choosing the splitting character
      6. Disabling whitespace trimming
      7. Reading process output
        1. Avoiding subshell problems
      8. Avoiding input problems with ssh
    6. Summary
  13. Scripts, Functions, and Aliases
    1. Aliases
      1. Defining new aliases
      2. Understanding shortcomings with aliases
    2. Functions
      1. Defining functions
      2. Passing arguments to functions
        1. Using --  to separate options from filenames
        2. Getting all the arguments
      3. Returning values from functions
      4. Understanding function input and output
      5. Function scope
      6. Reloading functions at shell startup
    3. Scripts
      1. Scripting methods
      2. Writing a shebang script
      3. Finding scripts with $PATH
      4. System bindir
      5. User bindir
      6. Arguments to scripts
      7. Understanding sh vs bash
      8. Using env
    4. Choosing between functions and scripts
    5. Using functions in scripts
    6. Summary
  14. Best Practices
    1. Quoting correctly
      1. When you don't want quotes
    2. Handling filenames starting with dashes
    3. Separating output and diagnostics
    4. Keeping scripts brief and simple
    5. Keeping scripts flexible
    6. Respecting and applying the user's configuration
    7. Allowing scripts to run without user input
    8. Limiting the scope of shell state changes
    9. Avoiding path anti-patterns
    10. Avoiding Bash for untrusted user input
    11. Documenting scripts
      1. Writing comments
      2. Providing help output
      3. Writing manual pages
    12. Using temporary files cleanly
    13. Cleaning up after a script
    14. Tools to check shell scripts for problems
    15. Summary
  15. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think