Cover image for Learning the bash Shell, Second Edition

Book description

The first thing users of the Linux operating system come face to face with is the shell. "Shell" is the UNIX term for a user interface to the system -- something that lets you communicate with the computer via the keyboard and display. Bash, the Free Software Foundation's "Bourne Again Shell," is the default shell for Linux, the popular free UNIX-like operating system. It's also a replacement for the standard UNIX Bourne shell, which serves both as a user interface and as a programming language. Like the FSF's other tools, bash is more than a mere replacement: it extends the Bourne shell in many ways. Features include command line editing, key bindings, integrated programming features, command completion, control structures (especially the select construct, which enables you to create menus easily), and new ways to customize your environment. Whether you want to use bash for its user interface or its programming features you will find Learning the bash Shell a valuable guide. The book covers all of bash's features, both for interactive use and programming. If you are new to shell programming, Learning the bash Shell provides an excellent introduction, covering everything from the most basic to the most advanced features, like signal handling and command line processing. If you've been writing shell scripts for years, it offers a great way to find out what the new shell offers. The book is full of examples of shell commands and programs that are designed to be useful in your everyday life as a user, not just to illustrate the feature being explained. All of these examples are freely available to you online on the Internet. This second edition covers all of the features of bash Version 2.0, while still applying to bash Version 1.x. New features include the addition of one-dimensional arrays, parameter expansion, and more pattern-matching operations. bash 2.0 provides even more conformity with POSIX.2 standards, and in POSIX.2 mode is completely POSIX.2 conformant. This second edition covers several new commands, security improvements, additions to ReadLine, improved configuration and installation, and an additional programming aid, the bash shell debugger. With this book you'll 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

Table of Contents

  1. Learning the bash Shell, 2nd Edition
    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. Acknowledgments for the First Edition
      9. Acknowledgments for the Second Edition
    2. 1. bash Basics
      1. What Is a Shell?
      2. Scope of This Book
      3. History of UNIX Shells
        1. The Bourne Again Shell
        2. Features of bash
      4. Getting bash
      5. Interactive Shell Use
        1. Commands, Arguments, and Options
      6. Files
        1. Directories
          1. The working directory
          2. Tilde notation
          3. Changing working directories
        2. Filenames, Wildcards, and Pathname Expansion
        3. Brace Expansion
      7. Input and Output
        1. Standard I/O
        2. I/O Redirection
        3. Pipelines
      8. Background Jobs
        1. Background I/O
        2. Background Jobs and Priorities
      9. Special Characters and Quoting
        1. Quoting
        2. Backslash-Escaping
        3. Quoting Quotation Marks
        4. Continuing Lines
        5. Control Keys
      10. Help
    3. 2. Command-Line Editing
      1. Enabling Command-Line Editing
      2. The History File
      3. emacs Editing Mode
        1. Basic Commands
        2. Word Commands
        3. Line Commands
        4. Moving Around in the History File
        5. Textual Completion
        6. Miscellaneous Commands
      4. vi Editing Mode
        1. Simple Control Mode Commands
        2. Entering and Changing Text
        3. Deletion Commands
        4. Moving Around in the History File
        5. Character-Finding Commands
        6. Textual Completion
        7. Miscellaneous Commands
      5. The fc Command
      6. History Expansion
      7. readline
        1. The readline Startup File
          1. readline variables
        2. Key Bindings Using bind
      8. Keyboard Habits
    4. 3. Customizing Your Environment
      1. The .bash_profile, .bash_logout, and .bashrc Files
      2. Aliases
      3. Options
        1. shopt
      4. Shell Variables
        1. Variables and Quoting
        2. Built-In Variables
          1. Editing mode variables
          2. Mail variables
          3. Prompting variables
          4. Command search path
          5. Command hashing
          6. Directory search path and variables
          7. Miscellaneous variables
      5. Customization and Subprocesses
        1. Environment Variables
          1. Terminal types
          2. Other common variables
        2. The Environment File
      6. Customization Hints
    5. 4. Basic Shell Programming
      1. Shell Scripts and Functions
        1. Functions
      2. Shell Variables
        1. Positional Parameters
          1. Positional parameters in functions
        2. Local Variables in Functions
        3. Quoting with $@ and $*
        4. More on Variable Syntax
      3. String Operators
        1. Syntax of String Operators
        2. Patterns and Pattern Matching
        3. Length Operator
      4. Command Substitution
      5. Advanced Examples: pushd and popd
    6. 5. Flow Control
      1. if/else
        1. Exit Status and Return
          1. Return
        2. Combinations of Exit Statuses
        3. Condition Tests
          1. String comparisons
          2. File attribute checking
        4. Integer Conditionals
      2. for
      3. case
      4. select
      5. while and until
    7. 6. Command-Line Options and Typed Variables
      1. Command-Line Options
        1. shift
        2. Options with Arguments
        3. getopts
      2. Typed Variables
      3. Integer Variables and Arithmetic
        1. Arithmetic Conditionals
        2. Arithmetic Variables and Assignment
      4. Arrays
    8. 7. Input/Output and Command-Line Processing
      1. I/O Redirectors
        1. Here-documents
        2. File Descriptors
      2. String I/O
        1. echo
          1. Options to echo
          2. echo escape sequences
        2. read
          1. Reading lines from files
          2. I/O redirection and multiple commands
          3. Command blocks
          4. Reading user input
      3. Command-Line Processing
        1. Quoting
        2. command, builtin, and enable
        3. eval
    9. 8. Process Handling
      1. Process IDs and Job Numbers
      2. Job Control
        1. Foreground and Background
        2. Suspending a Job
      3. Signals
        1. Control-Key Signals
        2. kill
        3. ps
          1. System V
          2. BSD
      4. trap
        1. Traps and Functions
        2. Process ID Variables and Temporary Files
        3. Ignoring Signals
        4. disown
        5. Resetting Traps
      5. Coroutines
        1. wait
        2. Advantages and Disadvantages of Coroutines
        3. Parallelization
      6. Subshells
        1. Subshell Inheritance
        2. Nested Subshells
      7. Process Substitution
    10. 9. Debugging Shell Programs
      1. Basic Debugging Aids
        1. Set Options
        2. Fake Signals
          1. EXIT
          2. DEBUG
      2. A bash Debugger
        1. Structure of the Debugger
          1. The driver script
          2. exec
        2. The Preamble
        3. Debugger Functions
          1. Commands
          2. Stepping
          3. Breakpoints
          4. Break conditions
          5. Execution tracing
          6. Debugger limitations
        4. A Sample bashdb Session
        5. Exercises
    11. 10. bash Administration
      1. Installing bash as the Standard Shell
        1. POSIX Mode
        2. Command-Line Options
      2. Environment Customization
        1. umask
        2. ulimit
        3. Types of Global Customization
      3. System Security Features
        1. Restricted Shell
        2. A System Break-In Scenario
        3. Privileged Mode
    12. 11. bash for Your System
      1. Obtaining bash
      2. Unpacking the Archive
      3. What’s in the Archive
        1. Documentation
        2. Configuring and Building bash
        3. Testing bash
        4. Potential Problems
        5. Installing bash as a Login Shell
        6. Examples
      4. Who Do I Turn to?
        1. Asking Questions
        2. Reporting Bugs
    13. A. Related Shells
      1. The Bourne Shell
      2. The IEEE 1003.2 POSIX Shell Standard
      3. The Korn Shell
      4. pdksh
      5. Workalikes on PC Platforms
    14. B. Reference Lists
      1. Invocation
      2. Built-In Commands and Reserved Words
      3. Environment Variables
      4. Test Operators
      5. set Options
      6. shopt Options
      7. I/O Redirection
      8. emacs Mode Commands
      9. vi Control Mode Commands
    15. C. Loadable Built-Ins
    16. D. Syntax
      1. Reserved Words
      2. BNF for bash
    17. E. Obtaining Sample Programs
      1. FTP
      2. FTPMAIL
    18. Index
    19. Colophon