Table of Contents for
Bash Quick Start Guide

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Bash Quick Start Guide by Tom Ryder Published by Packt Publishing, 2018
  1. Bash Quick Start Guide
  2. Title Page
  3. Copyright and Credits
  4. Bash Quick Start Guide
  5. Dedication
  6. Packt Upsell
  7. Why subscribe?
  8. Packt.com
  9. Contributors
  10. About the author
  11. About the reviewers
  12. Packt is searching for authors like you
  13. Table of Contents
  14. Preface
  15. Who this book is for
  16. What this book covers
  17. To get the most out of this book
  18. Download the example code files
  19. Download the color images
  20. Conventions used
  21. Get in touch
  22. Reviews
  23. What is Bash?
  24. What Bash is and is not
  25. Getting Bash
  26. Checking Bash is running
  27. Switching the login shell to Bash
  28. Identifying the Bash version number
  29. Upgrading Bash on macOS X
  30. Understanding Bash features
  31. POSIX shell script features
  32. Bash-specific features
  33. Do I need Bash?
  34. Choosing when to apply Bash
  35. Choosing when to avoid Bash
  36. Getting help with Bash
  37. Summary
  38. Bash Command Structure
  39. Using Bash interactively
  40. Interactive key bindings
  41. Simple commands
  42. Shell metacharacters
  43. Quoting
  44. Escaping
  45. Single quotes
  46. Double quotes
  47. Quote concatenation
  48. Running commands in sequence
  49. Exit values
  50. Stopping a command list on error
  51. Running a command in the background
  52. Summary
  53. Essential Commands
  54. Distinguishing command types
  55. Essential Bash builtin commands
  56. The type command
  57. The echo command
  58. The printf command
  59. The pwd command
  60. Tilde paths
  61. The cd command
  62. Changing the directory in scripts
  63. The set command
  64. The declare command
  65. The test, [, and [[ commands
  66. Essential system commands
  67. The ls command
  68. Getting filename lists without ls
  69. The mv command
  70. The cp command
  71. The rm and rmdir commands
  72. The grep command
  73. The cut command
  74. The wc command
  75. Getting file sizes with wc or du
  76. The find command
  77. Executing commands for each result
  78. A note about find and xargs
  79. The sort and uniq commands
  80. Summary
  81. Input, Output, and Redirection
  82. Redirecting output
  83. Redirection paths
  84. Avoiding overwrites
  85. Appending to files
  86. Understanding created file permissions
  87. Choosing permissions for created files
  88. Redirecting errors
  89. Combining errors with output
  90. Blocking errors completely
  91. Sending output to more than one place
  92. Redirecting input
  93. Using a long string as input with here-documents
  94. Using pipes
  95. Adding file contents to a stream
  96. Piping output from multiple programs
  97. Filtering programs
  98. The sed stream editor
  99. The AWK programming language
  100. Summary
  101. Variables and Patterns
  102. Using variables
  103. Listing variables
  104. Naming variables
  105. Variable name case
  106. Clearing variables
  107. Environment variables
  108. Calling programs with environment variables
  109. Expanding variables
  110. Reading a value into a variable
  111. Getting command output in variables
  112. Parameter expansion forms
  113. Specifying default values
  114. String chopping
  115. Extracting substrings
  116. Getting string length
  117. Substituting strings
  118. Changing case
  119. Combining parameter expansion forms
  120. Doing math in Bash
  121. Fixed or floating-point arithmetic
  122. Using globs
  123. Configuring globbing behavior
  124. Including dot files, but excluding dot and dot-dot
  125. Expanding to nothing
  126. Case-insensitive globbing
  127. Extended globbing
  128. Using arrays
  129. Glob expansion in arrays
  130. Associative arrays
  131. Summary
  132. Loops and Conditionals
  133. Using the if keyword
  134. Using the test command
  135. Using the [ command
  136. Using the [[ keyword
  137. Arithmetic conditions
  138. Switching with the case keyword
  139. Looping over shell words with for
  140. Skipping an iteration
  141. Ending the loop
  142. Misuse of for loops
  143. Using Bash's C-style for loops
  144. Using while loops
  145. Infinite loops
  146. Reading data line by line
  147. Field splitting
  148. Saving fields into arrays
  149. Choosing the splitting character
  150. Disabling whitespace trimming
  151. Reading process output
  152. Avoiding subshell problems
  153. Avoiding input problems with ssh
  154. Summary
  155. Scripts, Functions, and Aliases
  156. Aliases
  157. Defining new aliases
  158. Understanding shortcomings with aliases
  159. Functions
  160. Defining functions
  161. Passing arguments to functions
  162. Using --  to separate options from filenames
  163. Getting all the arguments
  164. Returning values from functions
  165. Understanding function input and output
  166. Function scope
  167. Reloading functions at shell startup
  168. Scripts
  169. Scripting methods
  170. Writing a shebang script
  171. Finding scripts with $PATH
  172. System bindir
  173. User bindir
  174. Arguments to scripts
  175. Understanding sh vs bash
  176. Using env
  177. Choosing between functions and scripts
  178. Using functions in scripts
  179. Summary
  180. Best Practices
  181. Quoting correctly
  182. When you don't want quotes
  183. Handling filenames starting with dashes
  184. Separating output and diagnostics
  185. Keeping scripts brief and simple
  186. Keeping scripts flexible
  187. Respecting and applying the user's configuration
  188. Allowing scripts to run without user input
  189. Limiting the scope of shell state changes
  190. Avoiding path anti-patterns
  191. Avoiding Bash for untrusted user input
  192. Documenting scripts
  193. Writing comments
  194. Providing help output
  195. Writing manual pages
  196. Using temporary files cleanly
  197. Cleaning up after a script
  198. Tools to check shell scripts for problems
  199. Summary
  200. Other Books You May Enjoy
  201. Leave a review - let other readers know what you think

Understanding created file permissions

When Bash creates a new file on the system for you into which output has been directed, it needs to decide what permissions to set for the new file. It decides what those permissions should be with reference to a value called umask for the current process, which can be set by the process' owner.

The logic for how this decision is made is a little involved, and may require a quick review of how permissions on Unix-like systems work. To start, let's create a new file using an output redirection, and examine it with ls -l:

$ printf 'Hello, world\n' > myfile $ ls -l myfile -rw-r--r-- 1 bashuser bashuser 1 2018-07-29 20:53:23 myfile

 

The permissions for the file are described in the the -rw-r--r-- string at the start of each line. This is a human-readable version of the permissions; each character is either a letter flag or a dash. It's possible that your own value may differ, depending on your system's default settings:

  • The first character of the string (-) is not a permission flag; it just shows the file type. In this case, a normal file (not a directory) shows a hyphen (-) in this field. A directory would show d instead.
  • The next three characters (rw-) are the file owner permissions: read, write, and execute. Since you have just created the file, its owner will generally be you. In this example, the owner is able to read from and write to the file, but not to execute it as a program.
  • The next three characters (r--) are the file group permissions: what a user in the file's group can do with the file if they're not the owner. In this case, members of our group can read the file, but can neither write to it nor execute it as a program.
  • The last three characters (r--) are the permissions for the world, or any other users who are neither the file's owner, nor in its group. Again, these users can read our file, but cannot write to it or execute it.

If we have GNU stat, we can also examine the permissions in another, more compact, format; a numeric notation with three octal numbers ranging from 0 to 7:

$ stat -c %a myfile
644

This value is calculated by adding 4 for each read bit if set, 2 for each write bit if set, and 1 for each execute bit if set:

  • The owner has read and write privileges, so the first value is 4 + 2, or 6
  • The group has read privileges only, so the second value is just 4
  • The world has read privileges only, so the third value is just 4

If we were to make the file executable by the owner, the owner permissions would add 1, and would then be 7, for numeric permissions of 744.

This numeric format is not as easy for humans to read as the symbolic format, but it is closer to how the computer represents the permissions for files.