TExcelTAD
Home › Linux & Bash › tee: Split Output

tee: Split Output

tee is named after the T-shaped pipe fitting: it reads its input and writes it both to the screen (standard output) and to one or more files. You use it to save a copy of a pipeline's output while still watching it, to log long-running commands and to write files that need root privileges.

Basic use

bash · try it yourself
echo "deploy started" | tee deploy.log
cat deploy.log
deploy started
deploy started

The text appeared on the screen and ended up in deploy.log.

Appending with -a

Like >, plain tee overwrites the file. -a appends, like >>:

bash · try it yourself
echo "step 1: build" | tee build.log
echo "step 2: test" | tee -a build.log
echo "step 3: package" | tee -a build.log > /dev/null
echo "--- build.log:"
cat build.log
step 1: build
step 2: test
--- build.log:
step 1: build
step 2: test
step 3: package

> /dev/null throws away tee's screen copy when you only want the file write; the third step was logged silently.

Saving the middle of a pipeline

Because tee passes its input through, you can put it between two commands and capture an intermediate result:

bash · try it yourself
cat > orders.txt <<'TXT'
ada 3
linus 1
grace 5
ada 2
TXT
sort orders.txt | tee sorted.txt | awk '{ s[$1] += $2 } END { for (n in s) print n, s[n] }' | sort
echo "--- intermediate sorted.txt:"
cat sorted.txt
ada 5
grace 5
linus 1
--- intermediate sorted.txt:
ada 2
ada 3
grace 5
linus 1

Several files at once

bash · try it yourself
printf 'line one\nline two\n' | tee copy1.txt copy2.txt | wc -l
cat copy1.txt copy2.txt | wc -l
2
4

Logging a script's output

A common pattern is to send everything a command prints, normal output and errors, through tee into a log file. 2>&1 merges errors into the output first:

bash · try it yourself
run_backup() {
  echo "copying files..."
  ls missing-folder
  echo "backup finished"
}
run_backup 2>&1 | tee backup.log
echo "--- lines in log: $(wc -l < backup.log)"
grep -c 'cannot access' backup.log
copying files...
ls: cannot access 'missing-folder': No such file or directory
backup finished
--- lines in log: 3
1

Note: In a pipeline, $? is the exit status of the last command, which is tee (almost always 0). If you need the status of the command before tee, use ${PIPESTATUS[0]} or set -o pipefail (see the error-handling lesson).

bash · try it yourself
ls missing-folder 2>/dev/null | tee out.txt
echo "\$? = $?, PIPESTATUS = ${PIPESTATUS[*]}"
$? = 0, PIPESTATUS = 2 0

Writing root-owned files: sudo tee

A classic puzzle: sudo echo "text" > /etc/file fails with "Permission denied". Why? The redirection > is done by your shell before sudo even starts, and your shell isn't root. tee solves it because it opens the file itself, and it runs under sudo:

bash
echo "127.0.0.1 shop.local" | sudo tee -a /etc/hosts
echo "vm.swappiness=10" | sudo tee /etc/sysctl.d/99-swap.conf > /dev/null

You can see the same rule as a normal user: a redirect into a directory you can't write fails, no matter what command it's attached to:

bash · try it yourself
echo "test" > /etc/motd-test
echo "status: $?"
/w/main.sh: line 1: /etc/motd-test: Permission denied
status: 1

Test yourself

What does cmd | tee out.txt do?

  1. Saves the output in out.txt only
  2. Shows the output and saves it in out.txt
  3. Appends out.txt to the output
  4. Runs cmd twice

Which option makes tee append instead of overwrite?

  1. -o
  2. -a
  3. -i
  4. >>

Why does sudo echo hi > /etc/x fail while echo hi | sudo tee /etc/x works?

  1. echo can't run as root
  2. The > redirection is performed by your non-root shell; tee opens the file itself as root
  3. tee is faster
  4. sudo doesn't support echo