You are previewing Pro Bash Programming : Scripting the GNU/Linux Shell, Second Edition.
O'Reilly logo
Pro Bash Programming : Scripting the GNU/Linux Shell, Second Edition

Book Description

Pro Bash Programming teaches you how to effectively utilize the Bash shell in your programming. The Bash shell is a complete programming language, not merely a glue to combine external Linux commands. By taking full advantage of Shell internals, Shell programs can perform as snappily as utilities written in C or other compiled languages. And you will see how, without assuming Unix lore, you can write professional Bash 4.3 programs through standard programming techniques.

This second edition has updated for Bash 4.3, and many scripts have been rewritten to make them more idiomatically Bash, taking better advantage of features specific to Bash. It is easy to read, understand, and will teach you how to get to grips with Bash programming without drowning you in pages and pages of syntax.

Using this book you will be able to use the shell efficiently, make scripts run faster using expansion and external commands, and understand how to overcome many common mistakes that cause scripts to fail. This book is perfect for all beginning Linux and Unix system administrators who want to be in full control of their systems, and really get to grips with Bash programming.

Table of Contents

  1. Cover
  2. Title
  3. Copyright
  4. Dedication
  5. Contents at a Glance
  6. Contents
  7. About the Authors
  8. Acknowledgments
  9. Chapter 1: Hello, World: Your First Shell Program
    1. The Code
    2. The File
      1. The Naming of Scripts
      2. Selecting a Directory for the Script
      3. Creating the File and Running the Script
    3. Choosing and Using a Text Editor
    4. Building a Better “Hello, World!”
    5. Summary
      1. Commands
      2. Concepts
      3. Variables
    6. Exercises
  10. Chapter 2: Input, Output, and Throughput
    1. Parameter and Variables
      1. Positional Parameters
      2. Special*@#0$?_!- Parameters
      3. Variables
    2. Arguments and Options
    3. echo, and Why You Should Avoid It
    4. printf: Formatting and Printing Data
      1. Escape Sequences
      2. Format Specifiers
      3. Width Specification
      4. Printing to a Variable
    5. Line Continuation
    6. Standard Input /Output Streams and Redirection
      1. Redirection: >, >>, and <
    7. Reading Input
    8. Pipelines
    9. Command Substitution
    10. Summary
      1. Commands
      2. Concepts
    11. Exercises
  11. Chapter 3: Looping and Branching
    1. Exit Status
    2. Testing an Expression
      1. test, a.k.a. [ … ]
      2. [[ … ]]: Evaluate an Expression
      3. (( … )): Evaluate an Arithmetic Expression
    3. Conditional Execution
      1. if
      2. Conditional Operators, & and ||
      3. case
    4. Looping
      1. while
      2. until
      3. for
      4. break
      5. continue
    5. Summary
      1. Commands
      2. Concepts
    6. Exercises
  12. Chapter 4: Command-Line Parsing and Expansion
    1. Quoting
    2. Brace Expansion
    3. Tilde Expansion
    4. Parameter and Variable Expansion
    5. Arithmetic Expansion
    6. Command Substitution
    7. Word Splitting
    8. Pathname Expansion
    9. Process Substitution
    10. Parsing Options
    11. Summary
      1. Commands
    12. Exercises
  13. Chapter 5: Parameters and Variables
    1. The Naming of Variables
    2. The Scope of a Variable: Can You See It from Here?
    3. Shell Variables
    4. Parameter Expansion
      1. Bourne Shell
      2. POSIX Shell
      3. Bash
      4. Bash-4.0
    5. Positional Parameters
    6. Arrays
      1. Integer-Indexed Arrays
      2. Associative Arrays
    7. Summary
      1. Commands
      2. Concepts
  14. Chapter 6: Shell Functions
    1. Definition Syntax
    2. Compound Commands
    3. Getting Results
      1. Set Different Exit Codes
      2. Print the Result
      3. Place Results in One or More Variables
    4. Function Libraries
      1. Using Functions from Libraries
    5. Sample Script
    6. Summary
      1. Commands
    7. Exercises
  15. Chapter 7: String Manipulation
    1. Concatenation
      1. Repeat Character to a Given Length
    2. Processing Character by Character
      1. Reversal
    3. Case Conversion
    4. Comparing Contents Without Regard to Case
    5. Check for Valid Variable Name
    6. Insert One String into Another
      1. Examples
    7. Overlay
      1. Examples
    8. Trim Unwanted Characters
      1. Examples
    9. Index
    10. Summary
      1. Commands
      2. Functions
    11. Exercises
  16. Chapter 8: File Operations and Commands
    1. Reading a File
    2. External Commands
      1. cat
      2. head
      3. touch
      4. ls
      5. cut
      6. wc
    3. Regular Expressions
      1. grep
      2. sed
      3. awk
    4. File Name Expansion Options
      1. nullglob
      2. failglob
      3. dotglob
      4. extglob
      5. nocaseglob
      6. globstar
    5. Summary
      1. Shell Options
      2. External Commands
    6. Exercises
  17. Chapter 9: Reserved Words and Built-In Commands
    1. help, Display Information About Built-In Commands
    2. time, Print Time Taken for Execution of a Command
    3. read, Read a Line from an Input Stream
      1. -r, Read Backslashes Literally
      2. -e, Get Input with the readline Library
      3. -a, Read Words into an Array
      4. -d DELIM, Read Until DELIM Instead of a Newline
      5. -n NUM, Read a Maximum of NUM Characters
      6. -s, Do Not Echo Input Coming from a Terminal
      7. -p PROMPT:, Output PROMPT Without a Trailing Newline
      8. -t TIMEOUT, Only Wait TIMEOUT Seconds for Complete Input
      9. -u FD: Read from File Descriptor FD Instead of the Standard Input
      10. -i TEXT, Use TEXT as the Initial Text for Readline
      11. eval, Expand Arguments and Execute Resulting Command
      12. Poor Man’s Arrays
      13. Setting Multiple Variables from One Command
    4. type, Display Information About Commands
    5. builtin, Execute a Built-In Command
    6. command, Execute a Command or Display Information About Commands
    7. pwd, Print the Current Working Directory
      1. unalias, Remove One or More Aliases
    8. Deprecated Built-Ins
    9. Dynamically Loadable Built-Ins
    10. Summary
      1. Commands and Reserved Words
      2. Deprecated Commands
    11. Exercise
  18. Chapter 10: Writing Bug-Free Scripts and Debugging the Rest
    1. Prevention Is Better Than Cure
      1. Structure Your Programs
      2. Document Your Code
      3. Format Your Code Consistently
      4. The K.I.S.S. Principle
      5. Test as You Go
    2. Debugging a Script
    3. Summary
    4. Exercises
  19. Chapter 11: Programming for the Command Line
    1. Manipulating the Directory Stack
      1. cd
      2. pd
      3. cdm
      4. menu
    2. Filesystem Functions
      1. l
      2. lsr
      3. cp, mv
      4. md
    3. Miscellaneous Functions
      1. pr1
      2. calc
    4. Managing Man Pages
      1. sman
      2. sus
      3. k
    5. Games
      1. The fifteen Puzzle
    6. Summary
    7. Exercises
  20. Chapter 12: Runtime Configuration
    1. Defining Variables
    2. Command-Line Options and Arguments
    3. Menus
    4. Q&A Dialogue
    5. Configuration Files
    6. Scripts with Several Names
    7. Environment Variables
    8. All Together Now
      1. Script Information
      2. Default Configuration
      3. Screen Variables
      4. Function Definitions
      5. Parse Command-Line Options
      6. Bits and Pieces
    9. Summary
    10. Exercises
  21. Chapter 13: Data Processing
    1. Arrays
      1. Holes in an Indexed Array
      2. Using an Array for Sorting
      3. Two-Dimensional Grids
    2. Data File Formats
      1. Line-Based Records
      2. Block File Formats
    3. Summary
    4. Exercises
  22. Chapter 14: Scripting the Screen
    1. Teletypewriter vs. Canvas
    2. Stretching the Canvas
      1. Command Sequence Introducer
    3. Priming the Canvas
    4. Moving the Cursor
    5. Changing Rendition Modes and Colors
    6. Placing a Block of Text on the Screen
    7. Scrolling Text
    8. Rolling Dice
    9. Summary
    10. Exercises
  23. Chapter 15: Entry-Level Programming
    1. Single-Key Entry
      1. Function Library, key-funcs
    2. History in Scripts
    3. Sanity Checking
    4. Form Entry
    5. Reading the Mouse
    6. Summary
    7. Exercises
  24. Appendix A: Shell Variables
    1. BASH
    2. BASHPID
    3. BASH_ALIASES
    4. BASH_ARGC
    5. BASH_ARGV
    6. BASH_CMDS
    7. BASH_COMMAND
    8. BASH_EXECUTION_STRING
    9. BASH_LINENO
    10. BASH_REMATCH
    11. BASH_SOURCE
    12. BASH_SUBSHELL
    13. BASH_VERSINFO
    14. BASH_VERSION
    15. COMP_CWORD
    16. COMP_KEY
    17. COMP_LINE
    18. COMP_POINT
    19. COMP_TYPE
    20. COMP_WORDBREAKS
    21. COMP_WORDS
    22. DIRSTACK
    23. EUID
    24. FUNCNAME
    25. GROUPS
    26. HISTCMD
    27. HOSTNAME
    28. HOSTTYPE
    29. LINENO
    30. MACHTYPE
    31. OLDPWD
    32. OPTARG
    33. OPTIND
    34. OSTYPE
    35. PIPESTATUS
    36. PPID
    37. PWD
    38. RANDOM
    39. REPLY
    40. SECONDS
    41. SHELLOPTS
    42. SHLVL
    43. UID
    44. BASH_ENV
    45. CDPATH
    46. COLUMNS
    47. COMPREPLY
    48. EMACS
    49. FCEDIT
    50. FIGNORE
    51. GLOBIGNORE
    52. HISTCONTROL
    53. HISTFILE
    54. HISTFILESIZE
    55. HISTIGNORE
    56. HISTSIZE
    57. HISTTIMEFORMAT
    58. HOME
    59. HOSTFILE
    60. IFS
    61. IGNOREEOF
    62. INPUTRC
    63. LANG
    64. LC_ALL
    65. LC_COLLATE
    66. LC_CTYPE
    67. LC_MESSAGES
    68. LC_NUMERIC
    69. LINES
    70. MAIL
    71. MAILCHECK
    72. MAILPATH
    73. OPTERR
    74. PATH
    75. POSIXLY_CORRECT
    76. PROMPT_COMMAND
    77. PROMPT_DIRTRIM
    78. PS1
    79. PS2
    80. PS3
    81. PS4
    82. SHELL
    83. TIMEFORMAT
    84. TMOUT
    85. TMPDIR
    86. auto_resume
    87. histchars
  25. Index