• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
Advanced Bash-Scripting Guide
An in-depth exploration of the art of shell scripting
Mendel Cooper
<thegrendel@theriver.com>
5.5
23 November 2008
Revision History
Revision 5.3
05 May 2008

Revised by: mc
'GOLDENBERRY' release: Minor Update.
Revision 5.4

21 July 2008

Revised by: mc
'ANGLEBERRY' release: Major Update.
Revision 5.5

23 Nov 2008
Revised by: mc
'FARKLEBERRY' release: Minor Update.
This tutorial assumes no previous knowledge of scripting or programming, but progresses rapidly toward an
intermediate/advanced level of instruction . . . all the while sneaking in little nuggets of UNIX\u00ae wisdom and
lore. It serves as a textbook, a manual for self-study, and a reference and source of knowledge on shell
scripting techniques. The exercises and heavily-commented examples invite active reader participation, under
the 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 and
rendered HTML, may be downloaded from the author's home site. A pdf version is also available ( pdf mirror
site). See the change log for a revision history.

Dedication
For Anita, the source of all the magic
Table of Contents
Chapter 1. Why Shell Programming?...............................................................................................................1
Chapter 2. Starting Off With a Sha-Bang........................................................................................................3
2.1. Invoking the script............................................................................................................................6
2.2. Preliminary Exercises.......................................................................................................................6

Part 2. Basics.......................................................................................................................................................7 Chapter 3. Special Characters...........................................................................................................................8 Chapter 4. Introduction to Variables and Parameters..................................................................................28

4.1. Variable Substitution......................................................................................................................28 4.2. Variable Assignment.......................................................................................................................31 4.3. Bash Variables Are Untyped..........................................................................................................32 4.4. Special Variable Types...................................................................................................................33

Chapter 5. Quoting...........................................................................................................................................39
5.1. Quoting Variables...........................................................................................................................39
5.2. Escaping..........................................................................................................................................41
Chapter 6. Exit and Exit Status.......................................................................................................................47
Chapter 7. Tests................................................................................................................................................49

7.1. Test Constructs...............................................................................................................................49 7.2. File test operators............................................................................................................................56 7.3. Other Comparison Operators..........................................................................................................59 7.4. Nested if/then Condition Tests.......................................................................................................64 7.5. Testing Your Knowledge of Tests..................................................................................................65

Chapter 8. Operations and Related Topics....................................................................................................66
8.1. Operators.........................................................................................................................................66
8.2. Numerical Constants.......................................................................................................................72
Part 3. Beyond the Basics.................................................................................................................................74
Chapter 9. Variables Revisited........................................................................................................................75

9.1. Internal Variables............................................................................................................................75
9.2. Manipulating Strings.......................................................................................................................92
9.2.1. Manipulating strings using awk..........................................................................................100
9.2.2. Further Reference................................................................................................................100
9.3. Parameter Substitution..................................................................................................................101
9.4. Typing variables: declare or typeset.............................................................................................110
9.4.1. Another use for declare.......................................................................................................112
9.5. Indirect References.......................................................................................................................112
9.6. $RANDOM: generate random integer..........................................................................................116
9.7. The Double-Parentheses Construct...............................................................................................127

Advanced Bash-Scripting Guide
i
Table of Contents
Chapter 10. Loops and Branches..................................................................................................................129

10.1. Loops..........................................................................................................................................129 10.2. Nested Loops..............................................................................................................................142 10.3. Loop Control...............................................................................................................................143 10.4. Testing and Branching................................................................................................................146

Chapter 11. Command Substitution.............................................................................................................155 Chapter 12. Arithmetic Expansion................................................................................................................161 Chapter 13. Recess Time................................................................................................................................162 Part 4. Commands..........................................................................................................................................163 Chapter 14. Internal Commands and Builtins.............................................................................................171

14.1. Job Control Commands..............................................................................................................199
Chapter 15. External Filters, Programs and Commands...........................................................................204

15.1. Basic Commands........................................................................................................................204 15.2. Complex Commands...................................................................................................................209 15.3. Time / Date Commands..............................................................................................................219 15.4. Text Processing Commands........................................................................................................223 15.5. File and Archiving Commands...................................................................................................245 15.6. Communications Commands......................................................................................................263 15.7. Terminal Control Commands.....................................................................................................277 15.8. Math Commands.........................................................................................................................278 15.9. Miscellaneous Commands..........................................................................................................289

Chapter 16. System and Administrative Commands..................................................................................303
16.1. Analyzing a System Script..........................................................................................................332
Part 5. Advanced Topics.................................................................................................................................334
Chapter 17. Regular Expressions..................................................................................................................336
17.1. A Brief Introduction to Regular Expressions..............................................................................336
17.2. Globbing.....................................................................................................................................340
Chapter 18. Here Documents.........................................................................................................................342
18.1. Here Strings................................................................................................................................352
Chapter 19. I/O Redirection...........................................................................................................................355

19.1. Using exec...................................................................................................................................358 19.2. Redirecting Code Blocks............................................................................................................361 19.3. Applications................................................................................................................................366

Chapter 20. Subshells.....................................................................................................................................368
Advanced Bash-Scripting Guide
ii
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...