Bash Guide For Beginners: Machtelt Garrels
Bash Guide For Beginners: Machtelt Garrels
Bash Guide For Beginners: Machtelt Garrels
Machtelt Garrels
Garrels BVBA
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
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
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
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
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
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.
• 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
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