Every modern operating system has at least one shell, and some have many. Some shells are command lineoriented, such as the shell discussed in this book. Others are graphical, like Windows Explorer or the Macintosh Finder. Some users will interact with the shell only long enough to launch their favorite application, and then never emerge from that until they log off. But most users spend a significant amount of time using the shell. The more you know about your shell, the faster and more productive you can be.
Whether you are a system administrator, a programmer, or an end user, there are certainly occasions where a simple (or perhaps not so simple) shell script can save you time and effort, or facilitate consistency and repeatability for some important task. Even using an alias to change or shorten the name of a command you use often can have a significant effect. Well cover this and much more.
As with any general programming language, there is more than one way to do a given task in the shell. In some cases, there is only one best way, but in most cases there are at least two or three equally effective and efficient ways to write a solution. Which way you choose depends on your personal style, creativity, and familiarity with different commands and techniques. This is as true for us as authors as it is for you as the reader. In most cases we will choose a single method and implement it. In a few cases we may choose a particular method and explain why we think its the best. We may also occasionally show more than one equivalent solution so you can choose the one that best fits your needs and environment.
There is also sometimes a choice between a clever way to write some code, and a readable way. We will choose the readable way every time because experience has taught us that no matter how transparent you think your clever code is now, 6 or 18 months and 10 projects from now, you will be scratching your head asking yourself what you were thinking. Trust us: write clear code, and document ityoull thank yourself (and us) later.
Who Should Read This Book
This book is for anyone who uses a Unix or Linux system, as well as system administrators who may use several systems on any given day. With it, you will be able to create scripts that allow you to accomplish more, in less time, more easily, consistently, and repeatably than ever before.
Anyone? Yes. New users will appreciate the sections on automating repetitive tasks, making simple substitutions, and customizing their environment to be more friendly and perhaps behave in more familiar ways. Power users and administrators will find new and different solutions to common tasks and challenges. Advanced users will have a collection of techniques they can use at a moments notice to put out the latest fire, without having to remember every little detail of syntax.
Ideal readers include:
New Unix or Linux users who dont know much about the shell, but want to do more than point and click
Experienced Unix or Linux users and system administrators looking for quick answers to shell scripting questions
Programmers who work in a Unix or Linux (or even Windows) environment and want to be more productive
New Unix or Linux sysadmins, or those coming from a Windows environment who need to come up to speed quickly
Experienced Windows users and sysadmins who want a more powerful scripting environment
This book will only briefly cover basic and intermediate shell scriptingsee Learning the bash Shell, 3rd Edition, by Cameron Newham (OReilly) and Classic Shell Scripting by Nelson H. F. Beebe and Arnold Robbins (OReilly) for more in-depth coverage. Instead, our goal is to provide solutions to common problems, with a strong focus on the how to rather than the theory. We hope this book will save you time when figuring out solutions or trying to remember syntax. In fact, thats why we wrote this book: its what we wanted, one we could read through to get ideas, then refer to for practical working examples when needed. That way we wouldnt have to remember the subtle differences between the shell, Perl, C, and so forth.
This book assumes you have access to a Unix or Linux system (or see Recipes ) and are familiar with logging in, typing basic commands, and using a text editor. You do not have to be root to use the vast majority of the recipes, though there are a few, particularly dealing with installing bash, where root access will be needed.
About This Book
This book covers bash, the GNU Bourne Again Shell, which is a member of the family of shells that includes the original Bourne shell, sh, the Korn shell, ksh, and the public domain Korn shell, pdksh. While these and other shells such as dash and zsh are not specifically covered, odds are that most of the scripts will work pretty well with them.
You should be able to read this book cover to cover, and also just pick it up and read anything that catches your eye. But perhaps most importantly, we hope that when you have a question about how to do something or you need a hint, you will be able to easily find the right answeror something close enoughand save time and effort.
A great part of the Unix philosophy is to build simple tools that do one thing well, then combine them as needed. This combination of tools is often accomplished via a shell script because these commands, called pipelines, can be long or difficult to remember and type. Where appropriate, well cover the use of many of these tools in the context of the shell script as the glue that holds the pieces together to achieve the goal.
The first edition of this book was written using OpenOffice.org Writer running on whatever Linux or Windows machine happened to be handy, and kept in Subversion (see ). That source was later converted to DocBook for production.