Bash redirections

The test command { echo hallo; xxxxxxxx;} writes one line to stdout and one line to stderr.

andre@nairobi:~$ { echo hallo; xxxxxxxx;}
hallo
bash: xxxxxxxx: command not found

Let's see what happens to the output channels when redirect operators are used in bash.

Get rid of channels

Write stdout to null file, only stderr remains:
andre@nairobi:~$ { echo hallo; xxxxxxxx;} > /dev/null
bash: xxxxxxxx: command not found
Write stderr to null file, only stdout remains:
andre@nairobi:~$ { echo hallo; xxxxxxxx;} 2> /dev/null
hallo
Write both out and err to null file, nothing remains:
andre@nairobi:~$ { echo hallo; xxxxxxxx;} &> /dev/null

Merging output channels

The program tee simply copies its stdin
to stdout and also to the given file (foo.txt).
Afterwards the content of foo.txt is shown so we can see what was written to the pipe |.
The pipe will only redirect the stdout channel to the program on the right-hand side.

andre@nairobi:~$ { echo hallo; xxxxxxxx;} | tee foo.txt
bash: xxxxxxxx: command not found
hallo

andre@nairobi:~$ cat foo.txt
hallo
Put stdout to stderr channel. Nothing is written to stdout:
andre@nairobi:~$ { echo hallo; xxxxxxxx;} 1>&2 | tee foo.txt
hallo
bash: xxxxxxxx: command not found

andre@nairobi:~$ cat foo.txt
Put stderr to stdout channel. Both channels written to stdout:
andre@nairobi:~$ { echo hallo; xxxxxxxx;} 2>&1 | tee foo.txt
hallo
bash: xxxxxxxx: command not found

andre@nairobi:~$ cat foo.txt
hallo
bash: xxxxxxxx: command not found

Flipping output channels

Drop stdout, and write stderr to stdout channel instead:
andre@nairobi:~$ { echo hallo; xxxxxxxx;} 2>&1 > /dev/null | tee foo.txt
bash: xxxxxxxx: command not found

andre@nairobi:~$ cat foo.txt
bash: xxxxxxxx: command not found
Drop stderr, and write stdout to stderr channel instead:
andre@nairobi:~$ { echo hallo; xxxxxxxx;} >&2 2> /dev/null | tee foo.txt
hallo

andre@nairobi:~$ cat foo.txt
Swap stderr and stdout channels:

Using descriptor 3 as interim channel.

andre@nairobi:~$ { echo hallo; xxxxxxxx;} 3>&1 1>&2 2>&3 | tee foo.txt
hallo
bash: xxxxxxxx: command not found

andre@nairobi:~$ cat foo.txt
bash: xxxxxxxx: command not found