Bash output redirection
There are 3 built-in file descriptors: stdin, stdout and stderr (std=standard). (You can define your own, see the linked article.)
2>redirects stdout/stderr, where
>is taken as
&>redirects stdout and stderr = everything (caution: see below)
&> is functional as of Bash 4. This seems to result in a slightly differing behaviour when redirecting output in Ruby scripts.
cmd &> file, prefer
cmd > file 2>&1, which equals: "Redirect stdout of
file, and redirect stderr just where stdout is going", e.g.
command > /dev/null 2>&1.
Applied to files
>creates or overwrites a file
>>creates or appends to a file
<reads from a file, where
<is taken as
Note: You should be pretty sure of what a command is doing if you are going to wipe it's output.
- stdout to file:
ls -l > ls-l.txt
- stderr to file:
grep da * 2> grep-errors.txt
- one to another:
grep da * 1>&2
do_stuff &> /dev/null
command < input-file > output-file, reads from input-file and writes stdout to output-file