O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. And you will see how, without assuming Unix lore, you can write professional bash 4.0 programs through standard programming techniques.

  • Complete bash coverage

  • Teaches bash as a programming language

  • Helps you master bash 4.0 features

Table of Contents

  1. Cover Page
  2. Title Page
  3. Copyright
  4. Contents at a Glance
  5. Contents
  6. About the Author
  7. About the Technical Reviewer
  8. 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
  9. CHAPTER 1   Hello, World! Your First Shell Program
    1. The Code
    2. The File
    3. Choosing and Using a Text Editor
    4. Building a Better "Hello, World!"
    5. Summary
    6. Exercises
  10. CHAPTER 2   Input, Output, and Throughput
    1. Parameter and Variables
    2. Arguments and Options
    3. echo, and Why You Should Avoid It
    4. printf: Formatting and Printing Data
    5. Line Continuation
    6. Standard Input/Output Streams and Redirection
    7. Reading Input
    8. Pipelines
    9. Command Substitution
    10. Exercises
  11. CHAPTER 3   Looping and Branching
    1. Exit Status
    2. Testing an Expression
    3. Lists
    4. Conditional execution
    5. Looping
    6. while
    7. Summary
    8. 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
    12. Commands
    13. Exercises
  13. CHAPTER 5   Parameters and Variables
    1. The Scope of a Variable: Can You See It from Here?
    2. Shell Variables
    3. The Naming of Variables
    4. Parameter Expansion
    5. Positional Parameters
    6. Arrays
    7. Summary
    8. Exercises
  14. CHAPTER 6   Shell Functions
    1. Definition Syntax
    2. Compound Commands
    3. Getting Results
    4. Function Libraries
    5. Sample Script
    6. Summary
    7. Exercises
  15. CHAPTER 7   String Manipulation
    1. Concatenation
    2. Processing Character by Character
    3. Case Conversion
    4. Comparing Contents Without Regard to Case
    5. Check for Valid Variable Name
    6. Insert One String into Another
    7. Overlay
    8. Trim Unwanted Characters
    9. Index
    10. Summary
    11. Exercises
  16. CHAPTER 8   File Operations and Commands
    1. Reading a File
    2. External Commands
    3. Regular Expressions
    4. File Name Expansion Options
    5. Summary
    6. Shell Options
    7. External Commands
    8. Exercises
  17. CHAPTER 9   Reserved Words and Builtin Commands
    1. help, Display Information About Builtin Commands
    2. time, Print Time Taken for Execution of a Command
    3. read, Read a Line from an Input Stream
    4. Setting Multiple Variables from One Command
    5. type, Display Information About Commands
    6. builtin, Execute a Builtin Command
    7. command, Execute a Command or Display Information About Commands
    8. pwd, Print the Current Working Directory
    9. Deprecated Builtins
    10. Dynamically Loadable Builtins
    11. Summary
    12. Commands and Reserved Words
    13. Exercises
  18. CHAPTER 10   Writing Bug-Free Scripts and Debugging the Rest
    1. Prevention Is Better Than Cure
    2. Debugging a Script
    3. Summary
    4. Exercises
  19. CHAPTER 11   Programming for the Command Line
    1. Manipulating the Directory Stack
    2. Filesystem Functions
    3. Miscellaneous Functions
    4. Managing Man Pages
    5. Games
    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
    9. Summary
    10. Exercises
  21. CHAPTER 13   Data Processing
    1. Arrays
    2. Data File Formats
    3. Summary
    4. Exercises
  22. CHAPTER 14   Scripting the Screen
    1. Teletypewriter vs. Canvas
    2. Stretching the Canvas
    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
    2. History in Scripts
    3. Sanity Checking
    4. Form Entry
    5. Reading the Mouse
    6. Summary
    7. Exercises
  24. APPENDIX   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