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

Book Description

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.

Table of Contents

  1. Copyright
  2. About the Author
  3. About the Technical Reviewer
  4. Introduction
    1. Why the Shell?
    2. Just the Shell, Ma'am, Just the Shell
    3. A Brief History of sh
    4. Which Version of Bash?
    5. Who Will Benefit from This Book?
    6. What's in the Book?
    7. Downloading the Code
    8. Contacting the Author
  5. 1. Hello, World! Your First Shell Program
    1. 1.1. The Code
    2. 1.2. The File
      1. 1.2.1. The Naming of Scripts
      2. 1.2.2. Selecting a Directory for the Script
      3. 1.2.3. Creating the File and Running the Script
    3. 1.3. Choosing and Using a Text Editor
    4. 1.4. Building a Better "Hello, World!"
    5. 1.5. Summary
      1. 1.5.1. Commands
      2. 1.5.2. Concepts
      3. 1.5.3. Variables
    6. 1.6. Exercises
  6. 2. Input, Output, and Throughput
    1. 2.1. Parameter and Variables
      1. 2.1.1. Positional Parameters
      2. 2.1.2. Special *@#0$?_!- Parameters
      3. 2.1.3. Variables
    2. 2.2. Arguments and Options
    3. 2.3. echo, and Why You Should Avoid It
    4. 2.4. printf: Formatting and Printing Data
      1. 2.4.1. Escape Sequences
      2. 2.4.2. Format Specifiers
      3. 2.4.3. Width Specification
      4. 2.4.4. Printing to a Variable
    5. 2.5. Line Continuation
    6. 2.6. Standard Input/Output Streams and Redirection
      1. 2.6.1. Redirection: >, >>, and <
    7. 2.7. Reading Input
    8. 2.8. Pipelines
    9. 2.9. Command Substitution
    10. 2.10. Summary
      1. 2.10.1. Commands
      2. 2.10.2. Concepts
    11. 2.11. Exercises
  7. 3. Looping and Branching
    1. 3.1. Exit Status
    2. 3.2. Testing an Expression
      1. 3.2.1. test, aka [ ... ]
        1. 3.2.1.1. File Tests
        2. 3.2.1.2. Integer Tests
        3. 3.2.1.3. String Tests
      2. 3.2.2. [[ ... ]]: Evaluate an Expression
        1. 3.2.2.1. Enhancements over Test
      3. 3.2.3. (( ... )): Evaluate an Arithmetic Expression
    3. 3.3. Lists
    4. 3.4. Conditional execution
      1. 3.4.1. if
      2. 3.4.2. Conditional Operators, && and ||
      3. 3.4.3. case
    5. 3.5. Looping
      1. 3.5.1. while
      2. 3.5.2. until
      3. 3.5.3. for
      4. 3.5.4. break
      5. 3.5.5. continue
    6. 3.6. Summary
      1. 3.6.1. Commands
      2. 3.6.2. Concepts
    7. 3.7. Exercises
  8. 4. Command-Line Parsing and Expansion
    1. 4.1. Quoting
    2. 4.2. Brace Expansion
    3. 4.3. Tilde Expansion
    4. 4.4. Parameter and Variable Expansion
    5. 4.5. Arithmetic Expansion
    6. 4.6. Command Substitution
    7. 4.7. Word Splitting
    8. 4.8. Pathname Expansion
    9. 4.9. Process Substitution
    10. 4.10. Parsing Options
    11. 4.11. Summary
      1. 4.11.1. Commands
    12. 4.12. Exercises
  9. 5. Parameters and Variables
    1. 5.1. The Scope of a Variable: Can You See It from Here?
    2. 5.2. Shell Variables
    3. 5.3. The Naming of Variables
    4. 5.4. Parameter Expansion
      1. 5.4.1. Bourne Shell
        1. 5.4.1.1. ${var:-default} and ${var-default}: Use Default Values
      2. 5.4.2. POSIX Shell
        1. 5.4.2.1. ${#var}: Length of Variable's Contents
        2. 5.4.2.2. ${var%PATTERN}: Remove the Shortest Match from the End
        3. 5.4.2.3. ${var%%PATTERN}: Remove the Longest Match from the End
        4. 5.4.2.4. ${var#PATTERN}: Remove the Shortest Match from the Beginning
        5. 5.4.2.5. ${var##PATTERN}: Remove the Longest Match from the Beginning
      3. 5.4.3. Bash
        1. 5.4.3.1. ${var//PATTERN/STRING}: Replace All Instances of PATTERN with STRING
        2. 5.4.3.2. ${var:OFFSET:LENGTH}: Return a Substring of $var
      4. 5.4.4. Bash-4.0
    5. 5.5. Positional Parameters
    6. 5.6. Arrays
      1. 5.6.1. Integer-Indexed Arrays
        1. 5.6.1.1. Displaying Arrays
      2. 5.6.2. Associative Arrays
    7. 5.7. Summary
      1. 5.7.1. Commands
      2. 5.7.2. Concepts
    8. 5.8. Exercises
  10. 6. Shell Functions
    1. 6.1. Definition Syntax
    2. 6.2. Compound Commands
    3. 6.3. Getting Results
      1. 6.3.1. Set Different Exit Codes
      2. 6.3.2. Print the Result
      3. 6.3.3. Place Results in One or More Variables
    4. 6.4. Function Libraries
      1. 6.4.1. Using Functions from Libraries
    5. 6.5. Sample Script
    6. 6.6. Summary
      1. 6.6.1. Commands
    7. 6.7. Exercises
  11. 7. String Manipulation
    1. 7.1. Concatenation
      1. 7.1.1. Repeat Character to a Given Length
    2. 7.2. Processing Character by Character
      1. 7.2.1. Reversal
    3. 7.3. Case Conversion
    4. 7.4. Comparing Contents Without Regard to Case
    5. 7.5. Check for Valid Variable Name
    6. 7.6. Insert One String into Another
      1. 7.6.1. Examples
    7. 7.7. Overlay
      1. 7.7.1. Examples
    8. 7.8. Trim Unwanted Characters
      1. 7.8.1. Examples
    9. 7.9. Index
    10. 7.10. Summary
      1. 7.10.1. Commands
      2. 7.10.2. Functions
    11. 7.11. Exercises
  12. 8. File Operations and Commands
    1. 8.1. Reading a File
    2. 8.2. External Commands
      1. 8.2.1. cat
      2. 8.2.2. head
      3. 8.2.3. touch
      4. 8.2.4. ls
      5. 8.2.5. cut
      6. 8.2.6. wc
    3. 8.3. Regular Expressions
      1. 8.3.1. grep
      2. 8.3.2. sed
      3. 8.3.3. awk
    4. 8.4. File Name Expansion Options
      1. 8.4.1. nullglob
      2. 8.4.2. failglob
      3. 8.4.3. dotglob
      4. 8.4.4. extglob
        1. 8.4.4.1. ?(pattern-list)
        2. 8.4.4.2. *(pattern-list)
        3. 8.4.4.3. @(pattern-list)
        4. 8.4.4.4. +(pattern-list)
        5. 8.4.4.5. !(pattern-list)
      5. 8.4.5. nocaseglob
      6. 8.4.6. globstar
    5. 8.5. Summary
      1. 8.5.1. Shell Options
      2. 8.5.2. External Commands
    6. 8.6. Exercises
  13. 9. Reserved Words and Builtin Commands
    1. 9.1. help, Display Information About Builtin Commands
    2. 9.2. time, Print Time Taken for Execution of a Command
    3. 9.3. read, Read a Line from an Input Stream
      1. 9.3.1. -r, Read Backslashes Literally
      2. 9.3.2. -e, Get Input with the readline Library
      3. 9.3.3. -a, Read Words into an Array
      4. 9.3.4. -d DELIM, Read Until DELIM Instead of a Newline
      5. 9.3.5. -n NUM, Read a Maximum of NUM Characters
      6. 9.3.6. -s, Do Not Echo Input Coming from a Terminal
      7. 9.3.7. -p PROMPT:, Output PROMPT Without a Trailing Newline
      8. 9.3.8. -t TIMEOUT, Only Wait TIMEOUT Seconds for Complete Input
      9. 9.3.9. -u FD: Read from File Descriptor FD Instead of the Standard Input
      10. 9.3.10. -i TEXT, Use TEXT as the Initial Text for readline
      11. 9.3.11. eval, Expand Arguments and Execute Resulting Command
      12. 9.3.12. Poor Man's Arrays
      13. 9.3.13. Setting Multiple Variables from One Command
    4. 9.4. type, Display Information About Commands
    5. 9.5. builtin, Execute a Builtin Command
    6. 9.6. command, Execute a Command or Display Information About Commands
    7. 9.7. pwd, Print the Current Working Directory
      1. 9.7.1. unalias, Remove One or More Aliases
    8. 9.8. Deprecated Builtins
    9. 9.9. Dynamically Loadable Builtins
    10. 9.10. Summary
      1. 9.10.1. Commands and Reserved Words
      2. 9.10.2. Deprecated Commands
    11. 9.11. Exercises
  14. 10. Writing Bug-Free Scripts and Debugging the Rest
    1. 10.1. Prevention Is Better Than Cure
      1. 10.1.1. Structure Your Programs
        1. 10.1.1.1. Comments
        2. 10.1.1.2. Initialization of Variables
        3. 10.1.1.3. Function Definitions
        4. 10.1.1.4. Runtime Configuration and Options
        5. 10.1.1.5. Process Information
      2. 10.1.2. Document Your Code
      3. 10.1.3. Format Your Code Consistently
      4. 10.1.4. The K.I.S.S. Principle
        1. 10.1.4.1. Grouping Commands
      5. 10.1.5. Test As You Go
    2. 10.2. Debugging a Script
    3. 10.3. Summary
    4. 10.4. Exercises
  15. 11. Programming for the Command Line
    1. 11.1. Manipulating the Directory Stack
      1. 11.1.1. cd
      2. 11.1.2. pd
      3. 11.1.3. cdm
      4. 11.1.4. menu
    2. 11.2. Filesystem Functions
      1. 11.2.1. l
      2. 11.2.2. lsr
      3. 11.2.3. cp, mv
      4. 11.2.4. md
    3. 11.3. Miscellaneous Functions
      1. 11.3.1. pr1
      2. 11.3.2. calc
    4. 11.4. Managing Man Pages
      1. 11.4.1. sman
      2. 11.4.2. sus
      3. 11.4.3. k
    5. 11.5. Games
      1. 11.5.1. The Fifteen Puzzle
    6. 11.6. Summary
    7. 11.7. Exercises
  16. 12. Runtime Configuration
    1. 12.1. Defining Variables
    2. 12.2. Command-Line Options and Arguments
    3. 12.3. Menus
    4. 12.4. Q&A Dialogue
    5. 12.5. Configuration Files
    6. 12.6. Scripts with Several Names
    7. 12.7. Environment Variables
    8. 12.8. All Together Now
      1. 12.8.1. ## Script Information
      2. 12.8.2. ## Default Configuration
      3. 12.8.3. ## Screen Variables
      4. 12.8.4. ## Function Definitions
        1. 12.8.4.1. Function: die
        2. 12.8.4.2. Function: menu
        3. 12.8.4.3. Function: qa
        4. 12.8.4.4. Function: print_config
        5. 12.8.4.5. Function: readline
      5. 12.8.5. ## Parse Command-Line Options
      6. 12.8.6. ## Bits and Pieces
    9. 12.9. Summary
    10. 12.10. Exercises
  17. 13. Data Processing
    1. 13.1. Arrays
      1. 13.1.1. Holes in an Indexed Array
      2. 13.1.2. Using an Array for Sorting
        1. 13.1.2.1. Insertion Sort Function
        2. 13.1.2.2. Searching an Array
        3. 13.1.2.3. Reading an Array into Memory
      3. 13.1.3. Two-Dimensional Grids
        1. 13.1.3.1. Working with Single-String Grids
        2. 13.1.3.2. Two-Dimensional Grids Using Arrays
    2. 13.2. Data File Formats
      1. 13.2.1. Line-Based Records
        1. 13.2.1.1. DSV: Delimiter-Separated Values
        2. 13.2.1.2. Fixed-Length Fields
      2. 13.2.2. Block File Formats
    3. 13.3. Summary
    4. 13.4. Exercises
  18. 14. Scripting the Screen
    1. 14.1. Teletypewriter vs. Canvas
    2. 14.2. Stretching the Canvas
      1. 14.2.1. CSI: Command Sequence Introducer
    3. 14.3. Priming the Canvas
    4. 14.4. Moving the Cursor
    5. 14.5. Changing Rendition Modes and Colors
    6. 14.6. Placing a Block of Text on the Screen
    7. 14.7. Scrolling Text
    8. 14.8. Rolling Dice
    9. 14.9. Summary
    10. 14.10. Exercises
  19. 15. Entry-Level Programming
    1. 15.1. Single-Key Entry
      1. 15.1.1. Function Library, key-funcs
    2. 15.2. History in Scripts
    3. 15.3. Sanity Checking
    4. 15.4. Form Entry
    5. 15.5. Reading the Mouse
    6. 15.6. Summary
    7. 15.7. Exercises
  20. A. Shell Variables
    1. A.1. BASH
    2. A.2. BASHPID
    3. A.3. BASH_ALIASES
    4. A.4. BASH_ARGC
    5. A.5. BASH_ARGV
    6. A.6. BASH_CMDS
    7. A.7. BASH_COMMAND
    8. A.8. BASH_EXECUTION_STRING
    9. A.9. BASH_LINENO
    10. A.10. BASH_REMATCH
    11. A.11. BASH_SOURCE
    12. A.12. BASH_SUBSHELL
    13. A.13. BASH_VERSINFO
    14. A.14. BASH_VERSION
    15. A.15. COMP_CWORD
    16. A.16. COMP_KEY
    17. A.17. COMP_LINE
    18. A.18. COMP_POINT
    19. A.19. COMP_TYPE
    20. A.20. COMP_WORDBREAKS
    21. A.21. COMP_WORDS
    22. A.22. DIRSTACK
    23. A.23. EUID
    24. A.24. FUNCNAME
    25. A.25. GROUPS
    26. A.26. HISTCMD
    27. A.27. HOSTNAME
    28. A.28. HOSTTYPE
    29. A.29. LINENO
    30. A.30. MACHTYPE
    31. A.31. OLDPWD
    32. A.32. OPTARG
    33. A.33. OPTIND
    34. A.34. OSTYPE
    35. A.35. PIPESTATUS
    36. A.36. PPID
    37. A.37. PWD
    38. A.38. RANDOM
    39. A.39. REPLY
    40. A.40. SECONDS
    41. A.41. SHELLOPTS
    42. A.42. SHLVL
    43. A.43. UID
    44. A.44. BASH_ENV
    45. A.45. CDPATH
    46. A.46. COLUMNS
    47. A.47. COMPREPLY
    48. A.48. EMACS
    49. A.49. FCEDIT
    50. A.50. FIGNORE
    51. A.51. GLOBIGNORE
    52. A.52. HISTCONTROL
    53. A.53. HISTFILE
    54. A.54. HISTFILESIZE
    55. A.55. HISTIGNORE
    56. A.56. HISTSIZE
    57. A.57. HISTTIMEFORMAT
    58. A.58. HOME
    59. A.59. HOSTFILE
    60. A.60. IFS
    61. A.61. IGNOREEOF
    62. A.62. INPUTRC
    63. A.63. LANG
    64. A.64. LC_ALL
    65. A.65. LC_COLLATE
    66. A.66. LC_CTYPE
    67. A.67. LC_MESSAGES
    68. A.68. LC_NUMERIC
    69. A.69. LINES
    70. A.70. MAIL
    71. A.71. MAILCHECK
    72. A.72. MAILPATH
    73. A.73. OPTERR
    74. A.74. PATH
    75. A.75. POSIXLY_CORRECT
    76. A.76. PROMPT_COMMAND
    77. A.77. PROMPT_DIRTRIM
    78. A.78. PS1
    79. A.79. PS2
    80. A.80. PS3
    81. A.81. PS4
    82. A.82. SHELL
    83. A.83. TIMEFORMAT
    84. A.84. TMOUT
    85. A.85. TMPDIR
    86. A.86. auto_resume
    87. A.87. histchars