Advertisement
opexxx

Bash Redirections Cheat Sheet.txt

Aug 2nd, 2014
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 11.03 KB | None | 0 0
  1. .---------------------------------------------------------------------------.
  2. |                                                                           |
  3. |                      Bash Redirections Cheat Sheet                        |
  4. |                                                                           |
  5. +---------------------------------------------------------------------------+
  6. |                                                                           |
  7. | Created by Peteris Krumins (peter@catonmat.net)                           |
  8. | www.catonmat.net -- good coders code, great coders reuse                  |
  9. |                                                                           |
  10. +-----------------------------.---------------------------------------------+
  11. | Redirection                 | Description                                 |
  12. '-----------------------------'---------------------------------------------'
  13. | cmd > file                  | Redirect the standard output (stdout) of    |
  14. |                             | `cmd` to a file.                            |
  15. +-----------------------------'---------------------------------------------'
  16. | cmd 1> file                 | Same as `cmd > file`. 1 is the default file |
  17. |                             | descriptor for stdout.                      |
  18. +-----------------------------'---------------------------------------------'
  19. | cmd 2> file                 | Redirect the standard error (stderr) of     |
  20. |                             | `cmd` to a file. 2 is the default file      |
  21. |                             | descriptor for stderr.                      |
  22. +-----------------------------'---------------------------------------------'
  23. | cmd >> file                 | Append stdout of `cmd` to a file.           |
  24. +-----------------------------'---------------------------------------------'
  25. | cmd 2>> file                | Append stderr of `cmd` to a file.           |
  26. +-----------------------------'---------------------------------------------'
  27. | cmd &> file                 | Redirect stdout and stderr to a file.       |
  28. +-----------------------------'---------------------------------------------'
  29. | cmd > file 2>&1             | Another way to redirect both stdout and     |
  30. |                             | stderr of `cmd` to a file. This *is not*    |
  31. |                             | same as `cmd 2>&1 > file`.                  |
  32. |                             | Redirection order matters!                  |
  33. +-----------------------------'---------------------------------------------'
  34. | cmd > /dev/null             | Discard stdout of `cmd`.                    |
  35. +-----------------------------'---------------------------------------------'
  36. | cmd 2> /dev/null            | Discard stderr of `cmd`.                    |
  37. +-----------------------------'---------------------------------------------'
  38. | cmd &> /dev/null            | Discard stdout and stderr.                  |
  39. +-----------------------------'---------------------------------------------'
  40. | cmd < file                  | Redirect the contents of the file to the    |
  41. |                             | stdin of `cmd`.                             |
  42. +-----------------------------'---------------------------------------------'
  43. | cmd << EOL                  |                                             |
  44. | foo                         | Redirect a bunch of lines to the stdin.     |
  45. | bar                         | If 'EOL' is quoted, text is treated         |
  46. | baz                         | literally. This is called a here-document.  |
  47. | EOL                         |                                             |
  48. +-----------------------------'---------------------------------------------'
  49. | cmd <<- EOL                 |                                             |
  50. | <tab>foo                    | Redirect a bunch of lines to the stdin.     |
  51. | <tab><tab>bar               | The <tab>'s are ignored but not the         |
  52. | EOL                         | whitespace. Helpful for formatting.         |
  53. +-----------------------------'---------------------------------------------'
  54. | cmd <<< "string"            | Redirect a single line of text to stdin.    |
  55. |                             | This is called a here-string.               |
  56. +-----------------------------'---------------------------------------------'
  57. | exec 2> file                | Redirect stderr of all commands to a file   |
  58. |                             | forever.                                    |
  59. +-----------------------------'---------------------------------------------'
  60. | exec 3< file                | Open a file for reading using a custom fd.  |
  61. +-----------------------------'---------------------------------------------'
  62. | exec 3> file                | Open a file for writing using a custom fd.  |
  63. +-----------------------------'---------------------------------------------'
  64. | exec 3<> file               | Open a file for reading and writing using   |
  65. |                             | a custom file descriptor.                   |
  66. +-----------------------------'---------------------------------------------'
  67. | exec 3>&-                   | Close a file descriptor.                    |
  68. +-----------------------------'---------------------------------------------'
  69. | exec 4>&3                   | Make file descriptor 4 to be a copy of file |
  70. |                             | descriptor 3. (Copy fd 3 to 4.)             |
  71. +-----------------------------'---------------------------------------------'
  72. | exec 4>&3-                  | Copy file descriptor 3 to 4 and close fd 3  |
  73. +-----------------------------'---------------------------------------------'
  74. | echo "foo" >&3              | Write to a custom file descriptor.          |
  75. +-----------------------------'---------------------------------------------'
  76. | cat <&3                     | Read from a custom file descriptor.         |
  77. +-----------------------------'---------------------------------------------'
  78. | (cmd1; cmd2) > file         | Redirect stdout from multiple commands to a |
  79. |                             | file (using a sub-shell).                   |
  80. +-----------------------------'---------------------------------------------'
  81. | { cmd1; cmd2; } > file      | Redirect stdout from multiple commands to a |
  82. |                             | file (faster; not using a sub-shell).       |
  83. +-----------------------------'---------------------------------------------'
  84. | exec 3<> /dev/tcp/host/port | Open a TCP connection to host:port.         |
  85. +-----------------------------'---------------------------------------------'
  86. | exec 3<> /dev/udp/host/port | Open a UDP connection to host:port.         |
  87. +-----------------------------'---------------------------------------------'
  88. | cmd <(cmd1)                 | Redirect stdout of `cmd1` to an anonymous   |
  89. |                             | fifo, then pass the fifo to `cmd` as an     |
  90. |                             | argument. Useful when `cmd` doesn't read    |
  91. |                             | from stdin directly.                        |
  92. +-----------------------------'---------------------------------------------'
  93. | cmd < <(cmd1)               | Redirect stdout of `cmd1` to an anonymous   |
  94. |                             | fifo, then redirect the fifo to stdin of    |
  95. |                         ____' `cmd`. Best example:                        |
  96. |                        | diff <(find /path1 | sort) <(find /path2 | sort) |
  97. +------------------------'----.---------------------------------------------'
  98. | cmd <(cmd1) <(cmd2)         | Redirect stdout of `cmd1` `cmd2` to two     |
  99. |                             | anonymous fifos, then pass both fifos as    |
  100. |                             | arguments to \verb|cmd|.                    |
  101. +-----------------------------.---------------------------------------------'
  102. | cmd1 >(cmd2)                | Run `cmd2` with its stdin connected to an   |
  103. |                             | anonymous fifo, and pass the filename of    |
  104. |                             | the pipe as an argument to `cmd1`.          |
  105. +-----------------------------.---------------------------------------------'
  106. | cmd1 | cmd2                 | Redirect stdout of cmd1 to stdin of `cmd2`. |
  107. |                             | Pro-tip: This is the same as                |
  108. |                             | `cmd1 > >(cmd2)`, same as `cmd2 < <(cmd1)`, |
  109. |                             | same as `> >(cmd2) cmd1`, same as           |
  110. |                             | `< <(cmd1) cmd2`.                           |
  111. +-----------------------------'---------------------------------------------'
  112. | cmd1 |& cmd2                | Redirect stdout and stderr of `cmd1` to     |
  113. |                             | stdin of `cmd2` (bash 4.0+ only).           |
  114. |                             | Use `cmd1 2>&1 | cmd2` for older bashes.    |
  115. +-----------------------------'---------------------------------------------'
  116. | cmd | tee file              | Redirect stdout of `cmd` to a file and      |
  117. |                             | print it to screen.                         |
  118. +-----------------------------'---------------------------------------------'
  119. | exec {filew}> file          | Open a file for writing using a named file  |
  120. |                             | descriptor called `{filew}` (bash 4.1+)     |
  121. +-----------------------------'---------------------------------------------'
  122. | cmd 3>&1 1>&2 2>&3          | Swap stdout and stderr of `cmd`.            |
  123. +-----------------------------'---------------------------------------------'
  124. | cmd > >(cmd1) 2> >(cmd2)    | Send stdout of `cmd` to `cmd1` and stderr   |
  125. |                             | `cmd` to `cmd2`.                            |
  126. +-----------------------------'---------------------------------------------'
  127. | cmd1 | cmd2 | cmd3 | cmd4   | Find out the exit codes of all piped cmds.  |
  128. | echo ${PIPESTATUS[@]}       |                                             |
  129. +-----------------------------'---------------------------------------------'
  130. |                                                                           |
  131. | I explained each one of these redirections in my article All About Bash   |
  132. | Redirections:                                                             |
  133. |                                                                           |
  134. | http://www.catonmat.net/blog/bash-one-liners-explained-part-three         |
  135. |                                                                           |
  136. +---------------------------------------------------------------------------+
  137. |                                                                           |
  138. | Did I miss any redirections? Let me know! Email me peter@catonmat.net, or |
  139. | fork this cheat sheet on github:                                          |
  140. |                                                                           |
  141. | http://www.github.com/pkrumins/bash-redirections-cheat-sheet              |
  142. |                                                                           |
  143. `-( Released under GNU Free Document License )------------------------------'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement