/  598
 
Advanced Bash−Scripting Guide
An in−depth exploration of the art of shell scripting
Mendel Cooper
<thegrendel@theriver.com>
3.206 February 2005
Revision History
Revision 3.003 Oct 2004Revised by: mc'LOGANBERRY' release: Major update.Revision 3.114 Nov 2004Revised by: mc'BAYBERRY' release: Bugfix update.Revision 3.206 Feb 2005Revised by: mc'BLUEBERRY' release: Minor update.This tutorial assumes no previous knowledge of scripting or programming, but progresses rapidly toward anintermediate/advanced level of instruction
. . . all the while sneaking in little snippets of UNIX® wisdom and lore
. It serves as a textbook, a manual for self−study, and a reference and source of knowledge on shellscripting techniques. The exercises and heavily−commented examples invite active reader participation, underthe premise that
the only way to really learn scripting is to write scripts
.This book is suitable for classroom use as a general introduction to programming concepts.The latest update of this document, as an archived, bzip2−ed "tarball" including both the SGML source andrendered HTML, may be downloaded from the author's home site. A pdf version is also available. See thechange log for a revision history.
Dedication
For Anita, the source of all the magic
 
Table of Contents
Chapter 1. Why Shell Programming?...............................................................................................................1Chapter 2. Starting Off With a Sha−Bang.......................................................................................................3
2.1. Invoking the script............................................................................................................................62.2. Preliminary Exercises.......................................................................................................................6
Part 2. Basics.......................................................................................................................................................7Chapter 3. Special Characters...........................................................................................................................8Chapter 4. Introduction to Variables and Parameters..................................................................................25
4.1. Variable Substitution......................................................................................................................254.2. Variable Assignment.......................................................................................................................274.3. Bash Variables Are Untyped..........................................................................................................284.4. Special Variable Types...................................................................................................................29
Chapter 5. Quoting...........................................................................................................................................34Chapter 6. Exit and Exit Status.......................................................................................................................40Chapter 7. Tests................................................................................................................................................42
7.1. Test Constructs...............................................................................................................................427.2. File test operators............................................................................................................................487.3. Other Comparison Operators..........................................................................................................517.4. Nested if/then Condition Tests.......................................................................................................567.5. Testing Your Knowledgeof Tests..................................................................................................56
Chapter 8. Operations and Related Topics....................................................................................................58
8.1. Operators.........................................................................................................................................588.2. Numerical Constants.......................................................................................................................64
Part 3. Beyond the Basics.................................................................................................................................66Chapter 9. Variables Revisited........................................................................................................................67
9.1. Internal Variables............................................................................................................................679.2. Manipulating Strings.......................................................................................................................849.2.1. Manipulating strings using awk............................................................................................889.2.2. Further Discussion.................................................................................................................889.3. Parameter Substitution....................................................................................................................899.4. Typing variables: declare or typeset...............................................................................................979.5. Indirect References to Variables.....................................................................................................999.6. $RANDOM: generate random integer..........................................................................................1019.7. The DoubleParenthesesConstruct...............................................................................................110
Chapter 10. Loops andBranches..................................................................................................................112
10.1. Loops..........................................................................................................................................11210.2. Nested Loops..............................................................................................................................12210.3. Loop Control...............................................................................................................................123
Advanced Bash−Scripting Guidei
 
Table ofContents
Chapter 10. Loops and Branches
10.4. Testing and Branching................................................................................................................126
Chapter 11. Internal Commands and Builtins.............................................................................................134
11.1. Job Control Commands..............................................................................................................156
Chapter 12. External Filters, Programs and Commands...........................................................................160
12.1. Basic Commands........................................................................................................................16012.2. Complex Commands...................................................................................................................16412.3. Time / Date Commands..............................................................................................................17312.4. Text Processing Commands........................................................................................................17512.5. File and Archiving Commands...................................................................................................19212.6. Communications Commands......................................................................................................20712.7. Terminal Control Commands.....................................................................................................21612.8. Math Commands.........................................................................................................................21712.9. Miscellaneous Commands..........................................................................................................226
Chapter 13. System and Administrative Commands..................................................................................236Chapter 14. Command Substitution.............................................................................................................260Chapter 15. ArithmeticExpansion................................................................................................................265Chapter 16. I/O Redirection...........................................................................................................................266
16.1. Using exec...................................................................................................................................26816.2. Redirecting Code Blocks............................................................................................................27216.3. Applications................................................................................................................................276
Chapter 17. Here Documents.........................................................................................................................278
17.1. Here Strings................................................................................................................................286
Chapter 18. Recess Time................................................................................................................................288Part 4. Advanced Topics.................................................................................................................................289Chapter 19. Regular Expressions..................................................................................................................290
19.1. ABrief Introduction to Regular Expressions..............................................................................29019.2. Globbing.....................................................................................................................................293
Chapter 20. Subshells.....................................................................................................................................295Chapter 21. Restricted Shells.........................................................................................................................298Chapter 22. Process Substitution...................................................................................................................300Chapter 23. Functions....................................................................................................................................303
23.1. Complex Functions and Function Complexities.........................................................................30523.2. Local Variables...........................................................................................................................315
Advanced Bash−Scripting Guideii

Share & Embed

Add a Comment

Characters: ...