Bash Guide for Beginners

Machtelt Garrels


Table of Contents

Introduction
Why this guide?
Who should read this book?
New versions, translations and availability
Revision History
Contributions
Feedback
Copyright information
What do you need?
Conventions used in this document
Organization of this document
1. Bash and Bash scripts
Common shell programs
General shell functions
Shell types
Advantages of the Bourne Again SHell
Bash is the GNU shell
Features only found in bash
Executing commands
General
Shell built-in commands
Executing programs from a script
Building blocks
Shell building blocks
Developing good scripts
Properties of good scripts
Structure
Terminology
A word on order and logic
An example Bash script: mysystem.sh
Example init script
Summary
Exercises
2. Writing and debugging scripts
Creating and running a script
Writing and naming
script1.sh
Executing the script
Script basics
Which shell will run the script?
Adding comments
Debugging Bash scripts
Debugging on the entire script
Debugging on part(s) of the script
Summary
Exercises
3. The Bash environment
Shell initialization files
System-wide configuration files
Individual user configuration files
Changing shell configuration files
Variables
Types of variables
Creating variables
Exporting variables
Reserved variables
Special parameters
Script recycling with variables
Quoting characters
Why?
Escape characters
Single quotes
Double quotes
ANSI-C quoting
Locales
Shell expansion
General
Brace expansion
Tilde expansion
Shell parameter and variable expansion
Command substitution
Arithmetic expansion
Process substitution
Word splitting
File name expansion
Aliases
What are aliases?
Creating and removing aliases
More Bash options
Displaying options
Changing options
Summary
Exercises
4. Regular expressions
Regular expressions
What are regular expressions?
Regular expression metacharacters
Basic versus extended regular expressions
Examples using grep
What is grep?
Grep and regular expressions
Pattern matching using Bash features
Character ranges
Character classes
Summary
Exercises
5. The GNU sed stream editor
Introduction
What is sed?
sed commands
Interactive editing
Printing lines containing a pattern
Deleting lines of input containing a pattern
Ranges of lines
Find and replace with sed
Non-interactive editing
Reading sed commands from a file
Writing output files
Summary
Exercises
6. The GNU awk programming language
Getting started with gawk
What is gawk?
Gawk commands
The print program
Printing selected fields
Formatting fields
The print command and regular expressions
Special patterns
Gawk scripts
Gawk variables
The input field separator
The output separators
The number of records
User defined variables
More examples
The printf program
Summary
Exercises
7. Conditional statements
Introduction to if
General
Simple applications of if
More advanced if usage
if/then/else constructs
if/then/elif/else constructs
Nested if statements
Boolean operations
Using the exit statement and if
Using case statements
Simplified conditions
Initscript example
Summary
Exercises
8. Writing interactive scripts
Displaying user messages
Interactive or not?
Using the echo built-in command
Catching user input
Using the read built-in command
Prompting for user input
Redirection and file descriptors
File input and output
Summary
Exercises
9. Repetitive tasks
The for loop
How does it work?
Examples
The while loop
What is it?
Examples
The until loop
What is it?
Example
I/O redirection and loops
Input redirection
Output redirection
Break and continue
The break built-in
The continue built-in
Examples
Making menus with the select built-in
General
Submenus
The shift built-in
What does it do?
Examples
Summary
Exercises
10. More on variables
Types of variables
General assignment of values
Using the declare built-in
Constants
Array variables
Creating arrays
Dereferencing the variables in an array
Deleting array variables
Examples of arrays
Operations on variables
Arithmetic on variables
Length of a variable
Transformations of variables
Summary
Exercises
11. Functions
Introduction
What are functions?
Function syntax
Positional parameters in functions
Displaying functions
Examples of functions in scripts
Recycling
Setting the path
Remote backups
Summary
Exercises
12. Catching signals
Signals
Introduction
Usage of signals with kill
Traps
General
How Bash interprets traps
More examples
Summary
Exercises
A. Shell Features
Common features
Differing features
Glossary
Index

List of Figures

1. Bash Guide for Beginners front cover
2.1. script1.sh
3.1. Different prompts for different users
6.1. Fields in awk
7.1. Testing of a command line argument with if
7.2. Example using Boolean operators

List of Tables

1. Typographic and usage conventions
1.1. Overview of programming terms
2.1. Overview of set debugging options
3.1. Reserved Bourne shell variables
3.2. Reserved Bash variables
3.3. Special bash variables
3.4. Arithmetic operators
4.1. Regular expression operators
5.1. Sed editing commands
5.2. Sed options
6.1. Formatting characters for gawk
7.1. Primary expressions
7.2. Combining expressions
8.1. Escape sequences used by the echo command
8.2. Options to the read built-in
10.1. Options to the declare built-in
12.1. Control signals in Bash
12.2. Common kill signals
A.1. Common Shell Features
A.2. Differing Shell Features