Learning the Bash Shell Other Resources from O’Reilly
Total Page:16
File Type:pdf, Size:1020Kb
Learning the bash Shell Other resources from O’Reilly Related titles Classic Shell Scripting Learning the Korn Shell Unix Power Tools Linux in a Nutshell Unix in a Nutshell SSH, The Secure Shell: The Definitive Guide oreilly.com oreilly.com is more than a complete catalog of O’Reilly books. You’ll also find links to news, events, articles, weblogs, sample chapters, and code examples. oreillynet.com is the essential portal for developers interested in open and emerging technologies, including new platforms, pro- gramming languages, and operating systems. Conferences O’Reilly brings diverse innovators together to nurture the ideas that spark revolutionary industries. We specialize in document- ing the latest tools and systems, translating the innovator’s knowledge into useful skills for those in the trenches. Visit con- ferences.oreilly.com for our upcoming events. Safari Bookshelf (safari.oreilly.com) is the premier online refer- ence library for programmers and IT professionals. Conduct searches across more than 1,000 books. Subscribers can zero in on answers to time-critical questions in a matter of seconds. Read the books on your Bookshelf from cover to cover or sim- ply flip to the page you need. Try it today with a free trial. THIRD EDITION Learning the bash Shell Cameron Newham and Bill Rosenblatt Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo Learning the bash Shell, Third Edition by Cameron Newham and Bill Rosenblatt Copyright © 2005, 1998, 1995 O’Reilly Media, Inc. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (safari.oreilly.com). For more information, contact our corporate/insti- tutional sales department: (800) 998-9938 or [email protected]. Editor: Mike Loukides Production Editor: Colleen Gorman Cover Designer: Edie Freedman Interior Designer: David Futato Printing History: October 1995: First Edition. January 1998: Second Edition. April 2005: Third Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Learning the bash Shell, the image of a silver bass, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. This book uses RepKover™, a durable and flexible lay-flat binding. ISBN: 978-0-596-00965-6 [M] 42.5# [9/09] Table of Contents Preface . ix 1. bash Basics . 1 What Is a Shell? 2 Scope of This Book 2 History of UNIX Shells 3 Getting bash 5 Interactive Shell Use 6 Files 7 Input and Output 14 Background Jobs 17 Special Characters and Quoting 20 Help 26 2. Command-Line Editing . 27 Enabling Command-Line Editing 28 The History List 28 emacs Editing Mode 29 vi Editing Mode 36 The fc Command 45 History Expansion 47 readline 49 Keyboard Habits 53 3. Customizing Your Environment . 55 The .bash_profile, .bash_logout, and .bashrc Files 56 Aliases 57 v Options 60 Shell Variables 62 Customization and Subprocesses 75 Customization Hints 79 4. Basic Shell Programming . 81 Shell Scripts and Functions 81 Shell Variables 86 String Operators 91 Command Substitution 99 Advanced Examples: pushd and popd 103 5. Flow Control . 107 if/else 108 for 122 case 128 select 131 while and until 134 6. Command-Line Options and Typed Variables . 137 Command-Line Options 137 Typed Variables 145 Integer Variables and Arithmetic 146 Arrays 157 7. Input/Output and Command-Line Processing . 162 I/O Redirectors 162 String I/O 168 Command-Line Processing 180 8. Process Handling . 197 Process IDs and Job Numbers 198 Job Control 198 Signals 201 trap 207 Coroutines 214 Subshells 217 Process Substitution 219 vi | Table of Contents 9. Debugging Shell Programs . 221 Basic Debugging Aids 221 A bash Debugger 230 10. bash Administration . 251 Installing bash as the Standard Shell 251 Environment Customization 254 System Security Features 258 11. Shell Scripting . 261 What’s That Do? 261 Starting Up 263 Potential Problems 265 Don’t Use bash 266 12. bash for Your System . 267 Obtaining bash 267 Unpacking the Archive 268 What’s in the Archive 268 Who Do I Turn to? 273 A. Related Shells . 275 The Bourne Shell 276 The IEEE 1003.2 POSIX Shell Standard 278 The Korn Shell 280 pdksh 281 zsh 282 Shell Clones and Unix-like Platforms 282 B. Reference Lists . 285 Invocation 285 Prompt String Customizations 287 Built-In Commands and Reserved Words 288 Built-In Shell Variables 290 Test Operators 294 set Options 295 shopt Options 297 I/O Redirection 298 emacs Mode Commands 300 vi Control Mode Commands 302 Table of Contents | vii C. Loadable Built-Ins . 304 D. Programmable Completion . 309 Index . 313 viii | Table of Contents Preface The first thing users of the UNIX or Linux operating systems 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 the display. Shells are just separate programs that encapsulate the system, and, as such, there are many to choose from. Systems are usually set up with a “standard” shell that new users adopt without question. However, some of these standard shells are rather old and lack many fea- tures of the newer shells. This is a shame, because shells have a large bearing on your working environment. Since changing shells is as easy as changing hats, there is no reason not to change to the latest and greatest in shell technology. Of the many shells to choose from, this book introduces the Bourne Again shell (bash for short), a modern general-purpose shell. Other useful modern shells are the Korn shell (ksh) and the “Tenex C shell” (tcsh); both are also the subjects of O’Reilly handbooks. bash Versions This book is relevant to all versions of bash, although older versions lack some of the features of the most recent version.* You can easily find out which version you are using by typing echo $BASH_VERSION. The earliest public version of bash was 1.0, and the most recent is 3.0 (released in July 2004). If you have an older version, you might like to upgrade to the latest one. Chapter 12 shows you how to go about it. * Throughout this book we have clearly marked with footnotes the features that are not present in the earlier versions. ix This is the Title of the Book, eMatter Edition Copyright © 2009 O’Reilly & Associates, Inc. All rights reserved. Summary of bash Features bash is a backward-compatible evolutionary successor to the Bourne shell that includes most of the C shell’s major advantages as well as features from the Korn shell and a few new features of its own. Features appropriated from the C shell include: • Directory manipulation, with the pushd, popd, and dirs commands. • Job control, including the fg and bg commands and the ability to stop jobs with CTRL-Z. • Brace expansion, for generating arbitrary strings. • Tilde expansion, a shorthand way to refer to directories. • Aliases, which allow you to define shorthand names for commands or com- mand lines. • Command history, which lets you recall previously entered commands. bash’s major new features include: • Command-line editing, allowing you to use vi-oremacs-style editing commands on your command lines. • Key bindings that allow you to set up customized editing key sequences. • Integrated programming features: the functionality of several external UNIX commands, including test, expr, getopt, and echo, has been integrated into the shell itself, enabling common programming tasks to be done more cleanly and efficiently. • Control structures, especially the select construct, which enables easy menu generation. • New options and variables that give you more ways to customize your environment. • One dimensional arrays that allow easy referencing and manipulation of lists of data. • Dynamic loading of built-ins, plus the ability to write your own and load them into the running shell. Intended Audience This book is designed to address casual UNIX and Linux users who are just above the “raw beginner” level. You should be familiar with the process of logging in, entering commands, and doing simple things with files. Although Chapter 1 reviews concepts such as the tree-like file and directory scheme, you may find that it moves too quickly if you’re a complete neophyte. In that case, we recommend the O’Reilly handbook, Learning the UNIX Operating System, by Jerry Peek, Grace Todino, and John Strang. x | Preface This is the Title of the Book, eMatter Edition Copyright © 2009 O’Reilly & Associates, Inc. All rights reserved. If you’re an experienced user, you may wish to skip Chapter 1 altogether. But if your experience is with the C shell, you may find that Chapter 1 reveals a few subtle dif- ferences between the bash and C shells. No matter what your level of experience is, you will undoubtedly learn many things in this book that will make you a more productive bash user—from major features down to details at the “nook-and-cranny” level that you may not have been aware of. If you are interested in shell programming (writing shell scripts and functions that automate everyday tasks or serve as system utilities), you should also find this book useful.