Learning the bash Shell, 3rd Edition

Book description

O'Reilly's bestselling book on Linux's bash shell is at it again. Now that Linux is an established player both as a server and on the desktop Learning the bash Shell has been updated and refreshed to account for all the latest changes. Indeed, this third edition serves as the most valuable guide yet to the bash shell.As any good programmer knows, the first thing users of the Linux operating system come face to face with is the shell the UNIX term for a user interface to the system. In other words, it's what lets you communicate with the computer via the keyboard and display. Mastering the bash shell might sound fairly simple but it isn't. In truth, there are many complexities that need careful explanation, which is just what Learning the bash Shell provides.If you are new to shell programming, the book provides an excellent introduction, covering everything from the most basic to the most advanced features. And if you've been writing shell scripts for years, it offers a great way to find out what the new shell offers. Learning the bash Shell is also full of practical examples of shell commands and programs that will make everyday use of Linux that much easier. With this book, programmers will learn:

  • How to install bash as your login shell
  • The basics of interactive shell use, including UNIX file and directory structures, standard I/O, and background jobs
  • Command line editing, history substitution, and key bindings
  • How to customize your shell environment without programming
  • The nuts and bolts of basic shell programming, flow control structures, command-line options and typed variables
  • Process handling, from job control to processes, coroutines and subshells
  • Debugging techniques, such as trace and verbose modes
  • Techniques for implementing system-wide shell customization and features related to system security

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. bash Versions
    2. Summary of bash Features
    3. Intended Audience
    4. Code Examples
    5. Chapter Summary
    6. Conventions Used in This Handbook
    7. We’d Like to Hear from You
    8. Using Code Examples
    9. Safari Enabled
    10. Acknowledgments for the First Edition
    11. Acknowledgments for the Second Edition
    12. Acknowledgments for the Third Edition
  2. 1. bash Basics
    1. 1.1. What Is a Shell?
    2. 1.2. Scope of This Book
    3. 1.3. History of UNIX Shells
      1. 1.3.1. The Bourne Again Shell
      2. 1.3.2. Features of bash
    4. 1.4. Getting bash
    5. 1.5. Interactive Shell Use
      1. 1.5.1. Commands, Arguments, and Options
    6. 1.6. Files
      1. 1.6.1. Directories
        1. 1.6.1.1. The working directory
        2. 1.6.1.2. Tilde notation
        3. 1.6.1.3. Changing working directories
      2. 1.6.2. Filenames, Wildcards, and Pathname Expansion
      3. 1.6.3. Brace Expansion
    7. 1.7. Input and Output
      1. 1.7.1. Standard I/O
      2. 1.7.2. I/O Redirection
      3. 1.7.3. Pipelines
    8. 1.8. Background Jobs
      1. 1.8.1. Background I/O
      2. 1.8.2. Background Jobs and Priorities
    9. 1.9. Special Characters and Quoting
      1. 1.9.1. Quoting
      2. 1.9.2. Backslash-Escaping
      3. 1.9.3. Quoting Quotation Marks
      4. 1.9.4. Continuing Lines
      5. 1.9.5. Control Keys
    10. 1.10. Help
  3. 2. Command-Line Editing
    1. 2.1. Enabling Command-Line Editing
    2. 2.2. The History List
    3. 2.3. emacs Editing Mode
      1. 2.3.1. Basic Commands
      2. 2.3.2. Word Commands
      3. 2.3.3. Line Commands
      4. 2.3.4. Moving Around in the History List
      5. 2.3.5. Textual Completion
      6. 2.3.6. Miscellaneous Commands
    4. 2.4. vi Editing Mode
      1. 2.4.1. Simple Control Mode Commands
      2. 2.4.2. Entering and Changing Text
      3. 2.4.3. Deletion Commands
      4. 2.4.4. Moving Around in the History List
      5. 2.4.5. Character-Finding Commands
      6. 2.4.6. Textual Completion
      7. 2.4.7. Miscellaneous Commands
    5. 2.5. The fc Command
    6. 2.6. History Expansion
    7. 2.7. readline
      1. 2.7.1. The readline Startup File
        1. 2.7.1.1. readline variables
      2. 2.7.2. Key Bindings Using bind
    8. 2.8. Keyboard Habits
  4. 3. Customizing Your Environment
    1. 3.1. The .bash_profile, .bash_logout, and .bashrc Files
    2. 3.2. Aliases
    3. 3.3. Options
      1. 3.3.1. shopt
    4. 3.4. Shell Variables
      1. 3.4.1. Variables and Quoting
      2. 3.4.2. Built-In Variables
        1. 3.4.2.1. Editing mode variables
        2. 3.4.2.2. Mail variables
        3. 3.4.2.3. Prompting variables
        4. 3.4.2.4. Command search path
        5. 3.4.2.5. Command hashing
        6. 3.4.2.6. Directory search path and variables
        7. 3.4.2.7. Miscellaneous variables
    5. 3.5. Customization and Subprocesses
      1. 3.5.1. Environment Variables
        1. 3.5.1.1. Terminal types
        2. 3.5.1.2. Other common variables
      2. 3.5.2. The Environment File
    6. 3.6. Customization Hints
  5. 4. Basic Shell Programming
    1. 4.1. Shell Scripts and Functions
      1. 4.1.1. Functions
    2. 4.2. Shell Variables
      1. 4.2.1. Positional Parameters
        1. 4.2.1.1. Positional parameters in functions
      2. 4.2.2. Local Variables in Functions
      3. 4.2.3. Quoting with $@ and $*
      4. 4.2.4. More on Variable Syntax
    3. 4.3. String Operators
      1. 4.3.1. Syntax of String Operators
      2. 4.3.2. Patterns and Pattern Matching
      3. 4.3.3. Length Operator
      4. 4.3.4. Extended Pattern Matching
    4. 4.4. Command Substitution
    5. 4.5. Advanced Examples: pushd and popd
  6. 5. Flow Control
    1. 5.1. if/else
      1. 5.1.1. Exit Status
      2. 5.1.2. Return
      3. 5.1.3. Combinations of Exit Statuses
      4. 5.1.4. Condition Tests
        1. 5.1.4.1. String comparisons
        2. 5.1.4.2. File attribute checking
      5. 5.1.5. Integer Conditionals
    2. 5.2. for
    3. 5.3. case
    4. 5.4. select
    5. 5.5. while and until
  7. 6. Command-Line Options and Typed Variables
    1. 6.1. Command-Line Options
      1. 6.1.1. shift
      2. 6.1.2. Options with Arguments
      3. 6.1.3. getopts
    2. 6.2. Typed Variables
    3. 6.3. Integer Variables and Arithmetic
      1. 6.3.1. Arithmetic Conditionals
      2. 6.3.2. Arithmetic Variables and Assignment
      3. 6.3.3. Arithmetic for Loops
    4. 6.4. Arrays
  8. 7. Input/Output and Command-Line Processing
    1. 7.1. I/O Redirectors
      1. 7.1.1. Here-documents
      2. 7.1.2. File Descriptors
    2. 7.2. String I/O
      1. 7.2.1. echo
        1. 7.2.1.1. Options to echo
        2. 7.2.1.2. echo escape sequences
      2. 7.2.2. printf
        1. 7.2.2.1. Additional bash printf specifiers
      3. 7.2.3. read
        1. 7.2.3.1. Reading lines from files
        2. 7.2.3.2. I/O redirection and multiple commands
        3. 7.2.3.3. Command blocks
        4. 7.2.3.4. Reading user input
    3. 7.3. Command-Line Processing
      1. 7.3.1. Quoting
      2. 7.3.2. command, builtin, and enable
      3. 7.3.3. eval
  9. 8. Process Handling
    1. 8.1. Process IDs and Job Numbers
    2. 8.2. Job Control
      1. 8.2.1. Foreground and Background
      2. 8.2.2. Suspending a Job
    3. 8.3. Signals
      1. 8.3.1. Control-Key Signals
      2. 8.3.2. kill
      3. 8.3.3. ps
        1. 8.3.3.1. System V
        2. 8.3.3.2. BSD
    4. 8.4. trap
      1. 8.4.1. Traps and Functions
      2. 8.4.2. Process ID Variables and Temporary Files
      3. 8.4.3. Ignoring Signals
      4. 8.4.4. disown
      5. 8.4.5. Resetting Traps
    5. 8.5. Coroutines
      1. 8.5.1. wait
      2. 8.5.2. Advantages and Disadvantages of Coroutines
      3. 8.5.3. Parallelization
    6. 8.6. Subshells
      1. 8.6.1. Subshell Inheritance
      2. 8.6.2. Nested Subshells
    7. 8.7. Process Substitution
  10. 9. Debugging Shell Programs
    1. 9.1. Basic Debugging Aids
      1. 9.1.1. Set Options
      2. 9.1.2. Fake Signals
        1. 9.1.2.1. EXIT
        2. 9.1.2.2. ERR
        3. 9.1.2.3. DEBUG
        4. 9.1.2.4. RETURN
      3. 9.1.3. Debugging Variables
    2. 9.2. A bash Debugger
      1. 9.2.1. Structure of the Debugger
        1. 9.2.1.1. The driver script
        2. 9.2.1.2. exec
      2. 9.2.2. The Preamble
      3. 9.2.3. Debugger Functions
        1. 9.2.3.1. Commands
        2. 9.2.3.2. Stepping
        3. 9.2.3.3. Breakpoints
        4. 9.2.3.4. Break conditions
        5. 9.2.3.5. Execution tracing
        6. 9.2.3.6. Debugger limitations
      4. 9.2.4. A Sample bashdb Session
      5. 9.2.5. Exercises
  11. 10. bash Administration
    1. 10.1. Installing bash as the Standard Shell
      1. 10.1.1. POSIX Mode
      2. 10.1.2. Command-Line Options
    2. 10.2. Environment Customization
      1. 10.2.1. umask
      2. 10.2.2. ulimit
      3. 10.2.3. Types of Global Customization
    3. 10.3. System Security Features
      1. 10.3.1. Restricted Shell
      2. 10.3.2. A System Break-In Scenario
      3. 10.3.3. Privileged Mode
  12. 11. Shell Scripting
    1. 11.1. What’s That Do?
      1. 11.1.1. Comments
      2. 11.1.2. Variables and Constants
    2. 11.2. Starting Up
    3. 11.3. Potential Problems
    4. 11.4. Don’t Use bash
  13. 12. bash for Your System
    1. 12.1. Obtaining bash
    2. 12.2. Unpacking the Archive
    3. 12.3. What’s in the Archive
      1. 12.3.1. Documentation
      2. 12.3.2. Configuring and Building bash
      3. 12.3.3. Testing bash
      4. 12.3.4. Potential Problems
      5. 12.3.5. Installing bash as a Login Shell
      6. 12.3.6. Examples
    4. 12.4. Who Do I Turn to?
      1. 12.4.1. Asking Questions
      2. 12.4.2. Reporting Bugs
  14. A. Related Shells
    1. A.1. The Bourne Shell
    2. A.2. The IEEE 1003.2 POSIX Shell Standard
    3. A.3. The Korn Shell
    4. A.4. pdksh
    5. A.5. zsh
    6. A.6. Shell Clones and Unix-like Platforms
      1. A.6.1. Cygwin
      2. A.6.2. DJGPP
      3. A.6.3. MKS Toolkit
      4. A.6.4. AT&T UWIN
  15. B. Reference Lists
    1. B.1. Invocation
    2. B.2. Prompt String Customizations
    3. B.3. Built-In Commands and Reserved Words
    4. B.4. Built-In Shell Variables
    5. B.5. Test Operators
    6. B.6. set Options
    7. B.7. shopt Options
    8. B.8. I/O Redirection
    9. B.9. emacs Mode Commands
    10. B.10. vi Control Mode Commands
  16. C. Loadable Built-Ins
  17. D. Programmable Completion
  18. Index
  19. About the Author
  20. Colophon
  21. Copyright

Product information

  • Title: Learning the bash Shell, 3rd Edition
  • Author(s): Cameron Newham
  • Release date: March 2005
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9780596009656