# Bash Guide for Beginners

Machtelt
Garrels
Garrels BVBA

    tille wants no spam _at_ garrels dot be

Version 1.11 Last updated 20081227
Linux
Scripts
linux
Bash
guide
Guide
Exercises
exercises
bash
scripting
Scripting
awk
sed
variables
functions
loops
conditionals

## Introduction

### Why this guide?

The primary reason for writing this document is that a lot of readers feel the existing HOWTO to be too short and incomplete, while the Bash Scripting guide is too much of a reference work. There is nothing in between these two extremes. I also wrote this guide on the general principle that not enough free basic courses are available, though they should be.

This is a practical guide which, while not always being too serious, tries to give real-life instead of theoretical examples. I partly wrote it because I don't get excited with stripped down and over-simplified examples written by people who know what they are talking about, showing some really cool Bash feature so much out of its context that you cannot ever use it in practical circumstances. You can read that sort of stuff after finishing this book, which contains exercises and examples that will help you survive in the real world.

From my experience as UNIX/Linux user, system administrator and trainer, I know that people can have years of daily interaction with their systems, without having the slightest knowledge of task automation. Thus they often think that UNIX is not user-friendly, and even worse, they get the impression that it is slow and old-fashioned. This problem is another one that can be remedied by this guide.

### Who should read this book?

Everybody working on a UNIX or UNIX-like system who wants to make life easier on themselves, power users and sysadmins alike, can benefit from reading this book. Readers who already have a grasp of working the system using the command line will learn the ins and outs of shell scripting that ease execution of daily tasks. System administration relies a great deal on shell scripting; common tasks are often automated using simple scripts. This document is full of examples that will encourage you to write your own and that will inspire you to improve on existing scripts.

Prerequisites/not in this course:

- You should be an experienced UNIX or Linux user, familiar with basic commands, man pages and documentation

- Being able to use a text editor

- Understand system boot and shutdown processes, init and initscripts

- Create users and groups, set passwords

- Permissions, special modes

- Understand naming conventions for devices, partitioning, mounting/unmounting file systems

- Adding/removing software on your system

See Introduction to Linux (or your local TLDP mirror) if you haven't mastered one or more of these topics. Additional information can be found in your system documentation (man and info pages), or at the Linux Documentation Project.

### New versions, translations and availability

The most recent edition can be found at . You should find the same version at .

This guide is available in print from Fultus.com.

#### Bash Guide for Beginners front cover

Front cover of the Bash guide, red highlighted code on blue background.

This guide has been translated:

- Chinese translation at , by Wang Wei.

- Ukrainian translation at , by Yaroslav Fedevych and his team.

A french translation is in the making and will be linked to as soon as it is finished.

### Revision History

1.11 2008-12-27 MG Processed input from readers. 1.10 2008-06-06 MG address change 1.9 2006-10-10 MG Incorporated reader remarks, added index using DocBook tags. 1.8 2006-03-15 MG clarified example in Chap4, corrected here doc in chap9, general checks and correction of typos, added link to Chinese and Ukrainian translation, note and stuff to know about awk in chap6. 1.7 2005-09-05 MG Corrected typos in chapter 3, 6 and 7, incorporated user remarks, added a note in chap7. 1.6 2005-03-01 MG Minor debugging, added more keywords, info about new Bash 3.0, took out blank image. 1.0 2004-04-27 TM Initial release for LDP; more exercises, more markup, less errors and abuse; added glossary. 1.0-beta 2003-04-20 MG Pre-release

### Contributions

Thanks to all the friends who helped (or tried to) and to my husband; your encouraging words made this work possible. Thanks to all the people who submitted bug reports, examples and remarks - among many, many others:

- Hans Bol, one of the groupies

- Mike Sim, remarks on style

- Dan Richter, for array examples

- Gerg Ferguson, for ideas on the title

- Mendel Leo Cooper, for making room

- #linux.be, for keeping my feet on the ground

- Frank Wang, for his detailed remarks on all the things I did wrong ;-)

Special thanks to Tabatha Marshall, who volunteered to do a complete review and spell and grammar check. We make a great team: she works when I sleep. And vice versa ;-)

### Feedback

Missing information, missing links, missing characters, remarks? Mail it to

    tille wants no spam _at_ garrels dot be

the maintainer of this document.

### Copyright information

    * Copyright (c) 2002-2007, Machtelt Garrels
    * All rights reserved.
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions are met:
    *
    *     * Redistributions of source code must retain the above copyright
    *       notice, this list of conditions and the following disclaimer.
    *     * Redistributions in binary form must reproduce the above copyright
    *       notice, this list of conditions and the following disclaimer in the
    *       documentation and/or other materials provided with the distribution.
    *     * Neither the name of the author, Machtelt Garrels, nor the
    *       names of its contributors may be used to endorse or promote products
    *       derived from this software without specific prior written permission.
    *
    * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY
    * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS BE LIABLE FOR ANY
    * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The author and publisher have made every effort in the preparation of this book to ensure the accuracy of the information. However, the information contained in this book is offered without warranty, either express or implied. Neither the author nor the publisher nor any dealer or distributor will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book.

The logos, trademarks and symbols used in this book are the properties of their respective owners.

### What do you need?

`bash`, available from http://www.gnu.org/directory/GNU/. The Bash shell is available on nearly every Linux system, and can these days be found on a wide variety of UNIX systems.

Compiles easily if you need to make your own, tested on a wide variety of UNIX, Linux, MS Windows and other systems.

### Conventions used in this document

The following typographic and usage conventions occur in this text:

#### Typographic and usage conventions

Text type | Meaning
--- | ---
Quoted text | Quotes from people, quoted computer output.

    terminal view

Literal computer input and output captured from the terminal, usually rendered with a light grey background.

`command` | Name of a command that can be entered on the command line.
`VARIABLE` | Name of a variable or pointer to content of a variable, as in `$VARNAME`.
`option` | Option to a command, as in the `-a` option to the `ls` command.
`argument` | Argument to a command, as in read `man `ls``.

    command options
    arguments

Command synopsis or general usage, on a separated line.

`filename` | Name of a file or directory, for example Change to the `/usr/bin` directory.
Key | Keys to hit on the keyboard, such as type Q to quit.
Button | Graphical button to click, like the OK button.
Menu Choice | Choice to select from a graphical menu, for instance: Select Help About Mozilla in your browser.
Terminology | Important term or concept: The Linux kernel is the heart of the system.

    \

The backslash in a terminal view or command synopsis indicates an unfinished line. In other words, if you see a long command that is cut into multiple lines, \ means Don't press Enter yet!

See | link to related subject within this guide.
The author | Clickable link to an external web resource.

### Organization of this document

This guide discusses concepts useful in the daily life of the serious Bash user. While a basic knowledge of the usage of the shell is required, we start with a discussion of the basic shell components and practices in the first three chapters.

Chapters four to six are discussions of basic tools that are commonly used in shell scripts.

Chapters eight to twelve discuss the most common constructs in shell scripts.

All chapters come with exercises that will test your preparedness for the next chapter.

- : Bash basics: why Bash is so good, building blocks, first guidelines on developing good scripts.

- : Script basics: writing and debugging.

- : The Bash Environment: initialization files, variables, quoting characters, shell expansion order, aliases, options.

- : Regular expressions: an introduction.

- : Sed: an introduction to the sed line editor.

- :Awk: introduction to the awk programming language.

- : Conditional statements: constructs used in Bash to test conditions.

- : Interactive scripts: making scripts user-friendly, catching user input.

- : Executing commands repetitively: constructs used in Bash to automate command execution.

- : Advanced variables: specifying variable types, introduction to arrays of variables, operations on variables.

- : Functions: an introduction.

- : Catching signals: introduction to process signalling, trapping user-sent signals.

&chap1; &chap2; &chap3; &chap4; &chap5; &chap6; &chap7; &chap8; &chap9; &chap10; &chap11; &chap12; &app1; &gloss; &index-gloss;