Bash Guide For Beginners: Machtelt Garrels

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Bash Guide for Beginners

Machtelt Garrels
Garrels BVBA

<tille wants no spam _at_ garrels dot be>

Version 1.11 Last updated 20081227 Edition


Bash Guide for Beginners

Table of Contents
Introduction.........................................................................................................................................................1
1. Why this guide?...................................................................................................................................1
2. Who should read this book?.................................................................................................................1
3. New versions, translations and availability.........................................................................................2
4. Revision History..................................................................................................................................2
5. Contributions.......................................................................................................................................3
6. Feedback..............................................................................................................................................3
7. Copyright information.........................................................................................................................3
8. What do you need?...............................................................................................................................4
9. Conventions used in this document.....................................................................................................4
10. Organization of this document...........................................................................................................5

Chapter 1. Bash and Bash scripts......................................................................................................................6


1.1. Common shell programs...................................................................................................................6
1.1.1. General shell functions............................................................................................................6
1.1.2. Shell types...............................................................................................................................6
1.2. Advantages of the Bourne Again SHell............................................................................................7
1.2.1. Bash is the GNU shell.............................................................................................................7
1.2.2. Features only found in bash.....................................................................................................7
1.3. Executing commands......................................................................................................................12
1.3.1. General..................................................................................................................................12
1.3.2. Shell built-in commands........................................................................................................12
1.3.3. Executing programs from a script.........................................................................................13
1.4. Building blocks...............................................................................................................................13
1.4.1. Shell building blocks.............................................................................................................13
1.5. Developing good scripts.................................................................................................................15
1.5.1. Properties of good scripts......................................................................................................16
1.5.2. Structure................................................................................................................................16
1.5.3. Terminology..........................................................................................................................16
1.5.4. A word on order and logic.....................................................................................................16
1.5.5. An example Bash script: mysystem.sh..................................................................................17
1.5.6. Example init script.................................................................................................................18
1.6. Summary.........................................................................................................................................19
1.7. Exercises.........................................................................................................................................19

Chapter 2. Writing and debugging scripts.....................................................................................................21


2.1. Creating and running a script..........................................................................................................21
2.1.1. Writing and naming...............................................................................................................21
2.1.2. script1.sh................................................................................................................................22
2.1.3. Executing the script...............................................................................................................23
2.2. Script basics....................................................................................................................................24
2.2.1. Which shell will run the script?.............................................................................................24
2.2.2. Adding comments..................................................................................................................24
2.3. Debugging Bash scripts..................................................................................................................25
2.3.1. Debugging on the entire script..............................................................................................25
2.3.2. Debugging on part(s) of the script.........................................................................................26
2.4. Summary.........................................................................................................................................28
2.5. Exercises.........................................................................................................................................28

i
Bash Guide for Beginners

Table of Contents
Chapter 3. The Bash environment..................................................................................................................29
3.1. Shell initialization files...................................................................................................................29
3.1.1. System-wide configuration files............................................................................................29
3.1.2. Individual user configuration files........................................................................................31
3.1.3. Changing shell configuration files........................................................................................33
3.2. Variables.........................................................................................................................................34
3.2.1. Types of variables.................................................................................................................34
3.2.2. Creating variables..................................................................................................................37
3.2.3. Exporting variables................................................................................................................38
3.2.4. Reserved variables.................................................................................................................39
3.2.5. Special parameters.................................................................................................................41
3.2.6. Script recycling with variables..............................................................................................43
3.3. Quoting characters..........................................................................................................................44
3.3.1. Why?.....................................................................................................................................45
3.3.2. Escape characters..................................................................................................................45
3.3.3. Single quotes.........................................................................................................................45
3.3.4. Double quotes........................................................................................................................45
3.3.5. ANSI-C quoting....................................................................................................................46
3.3.6. Locales...................................................................................................................................46
3.4. Shell expansion...............................................................................................................................46
3.4.1. General..................................................................................................................................46
3.4.2. Brace expansion....................................................................................................................46
3.4.3. Tilde expansion.....................................................................................................................47
3.4.4. Shell parameter and variable expansion................................................................................47
3.4.5. Command substitution...........................................................................................................48
3.4.6. Arithmetic expansion............................................................................................................49
3.4.7. Process substitution...............................................................................................................50
3.4.8. Word splitting........................................................................................................................50
3.4.9. File name expansion..............................................................................................................51
3.5. Aliases.............................................................................................................................................51
3.5.1. What are aliases?...................................................................................................................51
3.5.2. Creating and removing aliases..............................................................................................52
3.6. More Bash options..........................................................................................................................53
3.6.1. Displaying options.................................................................................................................53
3.6.2. Changing options...................................................................................................................54
3.7. Summary.........................................................................................................................................55
3.8. Exercises.........................................................................................................................................55

Chapter 4. Regular expressions.......................................................................................................................56


4.1. Regular expressions........................................................................................................................56
4.1.1. What are regular expressions?...............................................................................................56
4.1.2. Regular expression metacharacters.......................................................................................56
4.1.3. Basic versus extended regular expressions...........................................................................57
4.2. Examples using grep.......................................................................................................................57
4.2.1. What is grep?.........................................................................................................................57
4.2.2. Grep and regular expressions................................................................................................58
4.3. Pattern matching using Bash features.............................................................................................60
4.3.1. Character ranges....................................................................................................................60

ii
Bash Guide for Beginners

Table of Contents
Chapter 4. Regular expressions
4.3.2. Character classes...................................................................................................................60
4.4. Summary.........................................................................................................................................61
4.5. Exercises.........................................................................................................................................61

Chapter 5. The GNU sed stream editor..........................................................................................................62


5.1. Introduction.....................................................................................................................................62
5.1.1. What is sed?...........................................................................................................................62
5.1.2. sed commands.......................................................................................................................62
5.2. Interactive editing...........................................................................................................................63
5.2.1. Printing lines containing a pattern.........................................................................................63
5.2.2. Deleting lines of input containing a pattern..........................................................................64
5.2.3. Ranges of lines......................................................................................................................64
5.2.4. Find and replace with sed......................................................................................................65
5.3. Non-interactive editing...................................................................................................................66
5.3.1. Reading sed commands from a file.......................................................................................66
5.3.2. Writing output files...............................................................................................................66
5.4. Summary.........................................................................................................................................67
5.5. Exercises.........................................................................................................................................68

Chapter 6. The GNU awk programming language........................................................................................69


6.1. Getting started with gawk...............................................................................................................69
6.1.1. What is gawk?.......................................................................................................................69
6.1.2. Gawk commands...................................................................................................................69
6.2. The print program...........................................................................................................................70
6.2.1. Printing selected fields..........................................................................................................70
6.2.2. Formatting fields...................................................................................................................71
6.2.3. The print command and regular expressions.........................................................................72
6.2.4. Special patterns......................................................................................................................72
6.2.5. Gawk scripts..........................................................................................................................73
6.3. Gawk variables................................................................................................................................73
6.3.1. The input field separator........................................................................................................73
6.3.2. The output separators............................................................................................................74
6.3.3. The number of records..........................................................................................................75
6.3.4. User defined variables...........................................................................................................76
6.3.5. More examples......................................................................................................................76
6.3.6. The printf program................................................................................................................77
6.4. Summary.........................................................................................................................................77
6.5. Exercises.........................................................................................................................................77

Chapter 7. Conditional statements..................................................................................................................79


7.1. Introduction to if.............................................................................................................................79
7.1.1. General..................................................................................................................................79
7.1.2. Simple applications of if........................................................................................................82
7.2. More advanced if usage..................................................................................................................84
7.2.1. if/then/else constructs............................................................................................................84
7.2.2. if/then/elif/else constructs......................................................................................................87
7.2.3. Nested if statements...............................................................................................................88

iii
Bash Guide for Beginners

Table of Contents
Chapter 7. Conditional statements
7.2.4. Boolean operations................................................................................................................88
7.2.5. Using the exit statement and if..............................................................................................89
7.3. Using case statements.....................................................................................................................90
7.3.1. Simplified conditions............................................................................................................90
7.3.2. Initscript example..................................................................................................................92
7.4. Summary.........................................................................................................................................92
7.5. Exercises.........................................................................................................................................93

Chapter 8. Writing interactive scripts............................................................................................................94


8.1. Displaying user messages...............................................................................................................94
8.1.1. Interactive or not?..................................................................................................................94
8.1.2. Using the echo built-in command.........................................................................................94
8.2. Catching user input.........................................................................................................................97
8.2.1. Using the read built-in command..........................................................................................97
8.2.2. Prompting for user input........................................................................................................98
8.2.3. Redirection and file descriptors.............................................................................................99
8.2.4. File input and output............................................................................................................101
8.3. Summary.......................................................................................................................................106
8.4. Exercises.......................................................................................................................................106

Chapter 9. Repetitive tasks............................................................................................................................108


9.1. The for loop...................................................................................................................................108
9.1.1. How does it work?...............................................................................................................108
9.1.2. Examples.............................................................................................................................108
9.2. The while loop..............................................................................................................................109
9.2.1. What is it?............................................................................................................................109
9.2.2. Examples.............................................................................................................................110
9.3. The until loop................................................................................................................................112
9.3.1. What is it?............................................................................................................................112
9.3.2. Example...............................................................................................................................112
9.4. I/O redirection and loops..............................................................................................................113
9.4.1. Input redirection..................................................................................................................113
9.4.2. Output redirection................................................................................................................113
9.5. Break and continue.......................................................................................................................114
9.5.1. The break built-in................................................................................................................114
9.5.2. The continue built-in...........................................................................................................115
9.5.3. Examples.............................................................................................................................116
9.6. Making menus with the select built-in..........................................................................................117
9.6.1. General................................................................................................................................117
9.6.2. Submenus............................................................................................................................118
9.7. The shift built-in...........................................................................................................................118
9.7.1. What does it do?..................................................................................................................118
9.7.2. Examples.............................................................................................................................119
9.8. Summary.......................................................................................................................................120
9.9. Exercises.......................................................................................................................................120

iv
Bash Guide for Beginners

Table of Contents
Chapter 10. More on variables......................................................................................................................121
10.1. Types of variables.......................................................................................................................121
10.1.1. General assignment of values............................................................................................121
10.1.2. Using the declare built-in..................................................................................................121
10.1.3. Constants...........................................................................................................................122
10.2. Array variables............................................................................................................................123
10.2.1. Creating arrays..................................................................................................................123
10.2.2. Dereferencing the variables in an array.............................................................................123
10.2.3. Deleting array variables.....................................................................................................124
10.2.4. Examples of arrays............................................................................................................124
10.3. Operations on variables...............................................................................................................126
10.3.1. Arithmetic on variables.....................................................................................................126
10.3.2. Length of a variable...........................................................................................................126
10.3.3. Transformations of variables.............................................................................................127
10.4. Summary.....................................................................................................................................129
10.5. Exercises.....................................................................................................................................129

Chapter 11. Functions....................................................................................................................................131


11.1. Introduction.................................................................................................................................131
11.1.1. What are functions?...........................................................................................................131
11.1.2. Function syntax.................................................................................................................131
11.1.3. Positional parameters in functions....................................................................................132
11.1.4. Displaying functions..........................................................................................................133
11.2. Examples of functions in scripts.................................................................................................133
11.2.1. Recycling...........................................................................................................................133
11.2.2. Setting the path..................................................................................................................134
11.2.3. Remote backups................................................................................................................134
11.3. Summary.....................................................................................................................................136
11.4. Exercises.....................................................................................................................................136

Chapter 12. Catching signals.........................................................................................................................137


12.1. Signals.........................................................................................................................................137
12.1.1. Introduction.......................................................................................................................137
12.1.2. Usage of signals with kill..................................................................................................138
12.2. Traps...........................................................................................................................................139
12.2.1. General..............................................................................................................................139
12.2.2. How Bash interprets traps.................................................................................................139
12.2.3. More examples..................................................................................................................140
12.3. Summary.....................................................................................................................................140
12.4. Exercises.....................................................................................................................................140

Appendix A. Shell Features............................................................................................................................142


A.1. Common features.........................................................................................................................142
A.2. Differing features.........................................................................................................................143

Glossary...........................................................................................................................................................146
A...........................................................................................................................................................146
B...........................................................................................................................................................146

v
Bash Guide for Beginners

Table of Contents
Glossary
C...........................................................................................................................................................146
D...........................................................................................................................................................147
E...........................................................................................................................................................148
F...........................................................................................................................................................148
G...........................................................................................................................................................148
H...........................................................................................................................................................149
I............................................................................................................................................................149
J............................................................................................................................................................149
K...........................................................................................................................................................150
L...........................................................................................................................................................150
M..........................................................................................................................................................150
N...........................................................................................................................................................151
P...........................................................................................................................................................152
Q...........................................................................................................................................................152
R...........................................................................................................................................................152
S...........................................................................................................................................................153
T...........................................................................................................................................................153
U...........................................................................................................................................................154
V...........................................................................................................................................................154
W..........................................................................................................................................................155
X...........................................................................................................................................................155
Z...........................................................................................................................................................156

Index.................................................................................................................................................................157
A...........................................................................................................................................................157
B...........................................................................................................................................................157
C...........................................................................................................................................................158
D...........................................................................................................................................................158
E...........................................................................................................................................................158
F...........................................................................................................................................................159
G...........................................................................................................................................................160
H...........................................................................................................................................................160
I............................................................................................................................................................160
J............................................................................................................................................................161
K...........................................................................................................................................................161
L...........................................................................................................................................................161
M..........................................................................................................................................................161
N...........................................................................................................................................................161
O...........................................................................................................................................................162
P...........................................................................................................................................................162
Q...........................................................................................................................................................162
R...........................................................................................................................................................162
S...........................................................................................................................................................163
T...........................................................................................................................................................164
U...........................................................................................................................................................164
V...........................................................................................................................................................164
W..........................................................................................................................................................165

vi
Bash Guide for Beginners

Table of Contents
Index
X...........................................................................................................................................................165
Y...........................................................................................................................................................165
Z...........................................................................................................................................................165

vii
Introduction
1. 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 principal 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 userfriendly, 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.

2. 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.

Introduction 1
Bash Guide for Beginners

3. New versions, translations and availability


The most recent edition can be found at http://tille.garrels.be/training/bash/. You should find the same version
at http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html.

This guide is available in print from Fultus.com.

Figure 1. Bash Guide for Beginners front cover

This guide has been translated:

• Chinese translation at http://xiaowang.net/bgb-cn/, by Wang Wei.


• Ukrainian translation at http://docs.linux.org.ua/index.php/LDP:Bash_beginners_guide, by Yaroslav
Fedevych and his team.

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

4. Revision History
Revision History
Revision 1.11 2008-12-27 Revised by: MG
Processed input from readers.
Revision 1.10 2008-06-06 Revised by: MG
address change
Revision 1.9 2006-10-10 Revised by: MG
Incorporated reader remarks, added index using DocBook tags.
Revision 1.8 2006-03-15 Revised by: 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.
Revision 1.7 2005-09-05 Revised by: MG
Corrected typos in chapter 3, 6 and 7, incorporated user remarks, added a note in chap7.

Introduction 2

You might also like