← All commands
THE COMMAND REFERENCE

tee

Copy what comes in to a file and to the screen at the same time.

Last updated

BusyBox

Implementation-level reference; examples are not certified for every installed version.

Overview

tee reads its standard input and writes it twice: to standard output and to each file you name. It is the pipe fitting that lets you save what flows through a pipeline without stopping the flow, like a T-shaped pipe, which is where the name comes from.

Background

The macOS manual says tee is expected to be compatible with IEEE Std 1003.2 (POSIX.2), and it does not give a date for its first appearance. Today the name covers GNU tee, the tee that macOS ships, and the tee applet in BusyBox. They agree on writing to files and on -a; the handling of interrupts and of write errors differs.

Examples

Show the text and save a copy of it in a file; cat shows what was saved:

echo hello | tee out.txt && cat out.txt

Save without showing the text, by sending standard output to the null device:

echo hello | tee out.txt > /dev/null && cat out.txt

Add to a file instead of replacing it with -a:

echo one | tee log.txt > /dev/null && echo two | tee -a log.txt > /dev/null && cat log.txt

Save to several files at once:

echo hi | tee a.txt b.txt > /dev/null && cat a.txt b.txt

Keep a copy of the middle of a pipeline. The file keeps the original order, while the pipeline sorts:

printf 'b\na\n' | tee unsorted.txt | sort && cat unsorted.txt

Options

-a
Append to the files instead of replacing them.

Common mistakes

Without -a, tee empties the file first, so tee log.txt loses what was in log.txt. sudo echo text > file fails because the shell, not sudo, opens the file; echo text | sudo tee file > /dev/null works because tee runs as the privileged user. tee copies only standard output of the earlier command; to include error messages, join them first with 2>&1.

Portability

Only the file names and -a are in the Common examples. -i to ignore interrupts is in POSIX, and GNU adds -p and --output-error, which are not on every system. The Common examples were executed on GNU coreutils 9.11, macOS system tools, and BusyBox 1.37.0 (see the development guide for the exact check).

About BusyBox tee

BusyBox bundles tee as an applet, and anything beyond the common options depends on how it was built. This page's examples ran on BusyBox 1.37.0.

BusyBox notes

BusyBox tee supports -a and -i. GNU long options depend on the build.

References

Original explanations, with upstream documentation for further reading. FreeBSD references are comparison material, not a claim of Apple version equivalence.