Search notes:

Shell command: tee

View a command's output and writing it to a file, too:
$ tee [option …] [file …]
$ echo xyz | tee /tmp/out.1 /tmp/out.2
xyz
$ cat /tmp/out.1 /tmp/out.2
xyz
xyz

Options

-a --append append to the given FILEs (i. e. do not overwrite)
-i --ignore-interrupts ignore interrupt signals
-p diagnose errors writing to non pipes
--output-error[=MODE] set behavior on write error. If --output-error is not specified, tee will exit immediately on error writing to a pipe, and diagnose errors writing to non pipe outputs.
--help
--version output version information and exit

MODE

The value of MODE determines the behavior with write errors on the output:
warn diagnose errors writing to any output
warn-nopipe diagnose errors writing to any output not a pipe
exit exit on error writing to any output
exit-nopipe exit on error writing to any output not a pipe
The default MODE for the -p option is warn-nopipe.

Using tee in combination with sudo

tee can be used in combination with sudo to create text-files owned by root in a variation of the following pipeline.
$ echo 'text text text' | sudo tee /etc/some-config-file
See also redirection with sudo.

$PIPESTATUS

cmd-bad

cmd-bad is a shell script that write some text to stdout and more text to stderr.
It exits with code 42.
#!/bin/bash

>&1 echo "cmd-bad is running"
>&2 echo "cmd-bad is failing"
exit 42
Github repository shell-commands, path: /tee/cmd-bad

teeing output of cmd-bad

The following example shows how the output both to stdout and stderror can be captured and written to a file while at the same time writing it also to the console.
Additionally, the variable $PIPESTATUS is used to query the exit status of the command (rather than tee's exit status.
#!/bin/bash

cmd-bad 2>&1 | tee cmd-bad.log
echo "\$PIPESTATUS = $PIPESTATUS / \$? = $?"
Github repository shell-commands, path: /tee/PIPESTATUS

See also

Shell commands

Index