2 n d E d i Bash ti o n Pocket Reference HELP FOR POWER USERS & SYS ADMINS Arnold Robbins SECOND EDITION Bash Pocket Reference Arnold Robbins Bash Pocket Reference by Arnold Robbins Copyright © 2016 Arnold Robbins. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebasto‐ pol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promo‐ tional use. Online editions are also available for most titles (http://safaribook‐ sonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or [email protected]. Editor: Andy Oram Production Editor: Kristen Brown Proofreader: Jasmine Kwityn Indexer: Arnold Robbins Interior Designer: David Futato Cover Designer: Karen Montgomery Illustrator: Rebecca Demarest March 2016: Second Edition Revision History for the Second Edition 2016-02-17: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781491941591 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Bash Pocket Reference, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author 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 intellec‐ tual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-491-94159-1 [M] Table of Contents The Bash Shell 1 Conventions 2 History 2 Overview of Features 3 Invoking the Shell 4 Command Exit Status 6 Syntax 7 Functions 23 Variables 25 Arithmetic Expressions 47 Command History 49 Programmable Completion 54 Job Control 59 Shell Options 60 Command Execution 66 Coprocesses 68 Restricted Shells 69 Built-In Commands 70 Resources 133 Acknowledgments 134 Acknowledgments from the First Edition 134 Index 135 iii The Bash Shell This pocket reference covers Bash, particularly version 4.4, the primary shell for GNU/Linux and Mac OS X. Bash is available for Solaris and the various BSD systems, and can be easily com‐ piled for just about any other Unix system. It can even be com‐ piled for OpenVMS! The following topics are covered: • History • Overview of features • Invoking the shell • Command exit status • Syntax • Functions • Variables • Arithmetic expressions • Command history • Programmable completion • Job control • Shell options • Command execution 1 • Coprocesses • Restricted shells • Built-in commands • Resources Conventions Filenames, command names, options, and inline examples are shown in constant width. Input that a user should type in exactly as-is is shown in constant width userinput. Text that should be replaced with real data in examples and syntax descriptions is shown in constant width replaceable. New terms and emphasized words and phrases are shown in italics. Finally, references of the form name(N) refer to the manual page for name in section N of the online manual (accessed via the man command). Shell variable values (including environ‐ ment variables) are indicated as $VAR. History The original Bourne shell distributed with V7 Unix in 1979 became the standard shell for writing shell scripts. The Bourne shell is still found in /bin/sh on many commercial Unix systems. It has not changed that much since its initial release, although it has seen modest enhancements over the years. The most notable new features added were the CDPATH variable and a built-in test command with System III (circa 1980), com‐ mand hashing and shell functions for System V Release 2 (circa 1984), and the addition of job control features for System V Release 4 (1989). Because the Berkeley C shell (csh) offered features that were more pleasant for interactive use, such as command history and job control, for a long time the standard practice in the Unix world was to use the Bourne shell for programming and the C shell for daily use. David Korn at Bell Labs was the first 2 | The Bash Shell developer to enhance the Bourne shell by adding csh-like features to it: history, job control, and additional programma‐ bility. Eventually, the Korn shell’s feature set surpassed both that of the Bourne and C shells, while remaining compatible with the former for shell programming. Today, the POSIX stan‐ dard defines the “standard shell” language and behavior based on the System V Bourne shell, with a selected subset of features from the Korn shell. The Free Software Foundation, in keeping with its goal to pro‐ duce a complete Unix work-alike system, developed a clone of the Bourne shell, written from scratch, named “Bash,” the Bourne-Again SHell. Over time, Bash has become a POSIX- compliant version of the shell with many additional features overlapping those of the Korn shell, but Bash is not an exact Korn shell clone. Today, Bash is probably the most widely used Bourne-derived shell. Overview of Features The Bash shell provides the following features: • Input/output redirection • Wildcard characters for filename abbreviation • Shell variables and options for customizing the environment • A built-in command set for writing shell programs • Shell functions, for modularizing tasks within a shell program • Job control • Command-line editing (using the command syntax of either vi or Emacs) • Access to previous commands (command history) and the ability to edit them • Integer arithmetic Overview of Features | 3 • Arrays and arithmetic expressions • Command-name abbreviation (aliasing) • Upwards compliance with POSIX • Internationalization facilities • An arithmetic for loop Invoking the Shell The command interpreter for the Bash shell (bash) can be invoked as follows: bash [options] [arguments] Bash can execute commands from a terminal, from a file (when the first argument is a script), or from standard input (if no arguments remain or if -s is specified). The shell automatically prints prompts if standard input is a terminal, or if -i is given on the command line. On many systems, /bin/sh is a link to Bash. When invoked as sh, Bash acts more like the traditional Bourne shell: login shells read /etc/profile and ~/.profile, and regular shells read $ENV, if it is set. Full details are available in the bash(1) manpage. Command-Line Options Almost all the single-letter command-line options may also be used with the built-in set command (see the entry for set on page 113). The options are: -c str Read commands from string str. -D, --dump-strings Print all $"…" strings in the program. 4 | The Bash Shell