Advanced Bash-Scripting Guide
Table of Contents
Advanced Bash-Scripting Guide.......................................................................................................................1
An in-depth exploration of the art of shell scripting................................................................................1
Mendel Cooper..................................................................................................................................1
Dedication............................................................................................................................................................3
Part 1. Introduction..........................................................................................................................................13
Chapter 1. Why Shell Programming?.............................................................................................................15
Notes......................................................................................................................................................16
Chapter 2. Starting Off With a Sha-Bang......................................................................................................17
2.1. Invoking the script.....................................................................................................................................21
Notes......................................................................................................................................................21
2.2. Preliminary Exercises................................................................................................................................23
Part 2. Basics.....................................................................................................................................................25
Chapter 3. Special Characters.........................................................................................................................27
Notes......................................................................................................................................................45
Chapter 4. Introduction to Variables and Parameters..................................................................................47
4.1. Variable Substitution.................................................................................................................................49
Notes......................................................................................................................................................51
4.2. Variable Assignment..................................................................................................................................53
4.3. Bash Variables Are Untyped.....................................................................................................................55
4.4. Special Variable Types..............................................................................................................................57
Notes......................................................................................................................................................61
Chapter 5. Quoting...........................................................................................................................................63
5.1. Quoting Variables......................................................................................................................................65
Notes......................................................................................................................................................66
5.2. Escaping......................................................................................................................................................69
Chapter 6. Exit and Exit Status.......................................................................................................................75
Chapter 7. Tests................................................................................................................................................77
Advanced Bash-Scripting Guide
i
Table of Contents
7.1. Test Constructs...........................................................................................................................................79
7.2. File test operators.......................................................................................................................................87
Notes......................................................................................................................................................89
7.3. Other Comparison Operators...................................................................................................................91
Notes......................................................................................................................................................96
7.4. Nested if/then Condition Tests..................................................................................................................97
7.5. Testing Your Knowledge of Tests.............................................................................................................99
Chapter 8. Operations and Related Topics..................................................................................................101
8.1. Operators..................................................................................................................................................103
Notes....................................................................................................................................................109
8.2. Numerical Constants...............................................................................................................................111
Part 3. Beyond the Basics...............................................................................................................................113
Chapter 9. Variables Revisited......................................................................................................................115
9.1. Internal Variables....................................................................................................................................117
Notes....................................................................................................................................................133
9.2. Manipulating Strings...............................................................................................................................135
9.2.1. Manipulating strings using awk.................................................................................................141
9.2.2. Further Reference.......................................................................................................................142
Notes..............................................................................................................................................142
9.3. Parameter Substitution...........................................................................................................................143
Notes....................................................................................................................................................151
9.4. Typing variables: declare or typeset......................................................................................................153
Notes....................................................................................................................................................155
9.5. Indirect References..................................................................................................................................157
9.6. $RANDOM: generate random integer...................................................................................................161
Notes....................................................................................................................................................171
9.7. The Double Parentheses Construct........................................................................................................173
Chapter 10. Loops and Branches..................................................................................................................175
Advanced Bash-Scripting Guide
ii
Table of Contents
10.1. Loops.......................................................................................................................................................177
Notes....................................................................................................................................................190
10.2. Nested Loops...........................................................................................................................................191
10.3. Loop Control..........................................................................................................................................193
Notes....................................................................................................................................................196
10.4. Testing and Branching..........................................................................................................................197
Chapter 11. Command Substitution.............................................................................................................205
Notes....................................................................................................................................................210
Chapter 12. Arithmetic Expansion................................................................................................................211
Chapter 13. Recess Time................................................................................................................................213
Part 4. Commands..........................................................................................................................................215
Chapter 14. Internal Commands and Builtins.............................................................................................223
14.1. Job Control Commands........................................................................................................................251
Notes....................................................................................................................................................254
Chapter 15. External Filters, Programs and Commands...........................................................................257
15.1. Basic Commands....................................................................................................................................259
Notes....................................................................................................................................................264
15.2. Complex Commands..............................................................................................................................265
Notes....................................................................................................................................................274
15.3. Time / Date Commands.........................................................................................................................275
15.4. Text Processing Commands..................................................................................................................279
Notes....................................................................................................................................................300
15.5. File and Archiving Commands.............................................................................................................301
Notes....................................................................................................................................................317
15.6. Communications Commands................................................................................................................319
Notes....................................................................................................................................................332
15.7. Terminal Control Commands...............................................................................................................333
15.8. Math Commands....................................................................................................................................335
Advanced Bash-Scripting Guide
iii