Bash Guide for Beginners

Machtelt Garrels


Table of Contents

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