I/O Redirection
===============

For reference see the [Advanced Bash-Scripting Guide](https://tldp.org/LDP/abs/html/io-redirection.html).

For background:
>     a number 1 = standard out (i.e. STDOUT)
>     a number 2 = standard error (i.e. STDERR)
>     if a number isn't explicitly given, then number 1 is assumed by the shell (bash)

Functions
---------

`2>&-`

The general form of this one is M>&-, where "M" is a file descriptor number.
This will close output for whichever file descriptor is referenced, i.e. "M".

`2>/dev/null`

The general form of this one is M>/dev/null, where "M" is a file descriptor number.
This will redirect the file descriptor, "M", to /dev/null.

`2>&1`

The general form of this one is M>&N, where "M" & "N" are file descriptor numbers.
It combines the output of file descriptors "M" and "N" into a single stream.

`|&`

This is just an abbreviation for 2>&1 |.

`&>/dev/null`

This is just an abbreviation for >/dev/null 2>&1.
It redirects file descriptor 2 (STDERR) and descriptor 1 (STDOUT) to /dev/null.

`>/dev/null`

This is just an abbreviation for 1>/dev/null.
It redirects file descriptor 1 (STDOUT) to /dev/null.

3.1 Theory and quick reference

There are 3 file descriptors, stdin, stdout and stderr (std=standard).

Basically you can:

    redirect stdout to a file
    redirect stderr to a file
    redirect stdout to a stderr
    redirect stderr to a stdout
    redirect stderr and stdout to a file
    redirect stderr and stdout to stdout
    redirect stderr and stdout to stderr

1 'represents' stdout and 2 stderr.

A little note for seeing this things: with the less command you can view both stdout (which will remain on the buffer) and the stderr that will be printed on the screen, but erased as you try to 'browse' the buffer.

3.2 Sample: stdout 2 file

This will cause the ouput of a program to be written to a file.

```
ls -l > ls-l.txt
```

Here, a file called 'ls-l.txt' will be created and it will contain what you would see on the screen if you type the command 'ls -l' and execute it.

3.3 Sample: stderr 2 file

This will cause the stderr ouput of a program to be written to a file.

```
grep da * 2> grep-errors.txt
```

Here, a file called 'grep-errors.txt' will be created and it will contain what you would see the stderr portion of the output of the 'grep da *' command.

3.4 Sample: stdout 2 stderr

This will cause the stderr ouput of a program to be written to the same filedescriptor than stdout.

```
grep da * 1>&2 
```

Here, the stdout portion of the command is sent to stderr, you may notice that in differen ways.

3.5 Sample: stderr 2 stdout

This will cause the stderr ouput of a program to be written to the same filedescriptor than stdout.

```
grep * 2>&1
```

Here, the stderr portion of the command is sent to stdout, if you pipe to less, you'll see that lines that normally 'dissapear' (as they are written to stderr) are being kept now (because they're on stdout).

3.6 Sample: stderr and stdout 2 file

This will place every output of a program to a file. This is suitable sometimes for cron entries, if you want a command to pass in absolute silence.

```
rm -f $(find / -name core) &> /dev/null 
```
