Redirection in Bash

> redirects output read from a file descriptor to a file. Default descriptor is 1 (stdout), so >name means 1>name. Example:
| |
< redirects input. So wc < file.txt means wc will treat data from file.txt as stdin.
2>& redirects file handle “2” (almost always stderr) to some other file handle (it’s generally written as 2>&1, which redirects stderr to the same place as stdout).
&> and >& redirect both stdout and stderr to a file. It’s normally written as &>file (or >&file). It’s functionally the same as >file 2>&1. (todo: so why do they have different syntax?)
&>file is equivalent to 1>file 2>&1.
# Notes
- It seems like you can ignore spacing between redirection symbols. So
ls > fileandls>fileare the same. But when dealing with descriptors and stuff - it should be without spaces (2>). - Files are read/created at first and then the commands are executed. So
ls > filewill writefileinfile.
# See also
# Links
- bash redirects - wizardzines
- Read and write data from anywhere with redirection in the Linux terminal | Opensource.com
- Input Output Redirection in Linux/Unix Examples
- What does &> do in bash? - Stack Overflow
- What’s the difference of redirect an output using “>”, “&>”, “>&” and “2&>”? - Stack Overflow
More advanced: