← Back to Bash Course | Chapter 3: Control Flow | Lesson 11 of 14

tee

tee is like a T-shaped water pipe: the same water flows both to a bucket and onward to the next place. You save the output to a file and still see it on screen.

In this page:

  1. tee
Syntax
bash
command | tee file_name
command | tee -a file_name

tee

tee reads from standard input and writes it both to a file and back out to standard output simultaneously, letting you save output to a log while still seeing it in your terminal.

It's often placed in the middle of a pipeline. tee -a appends to the file instead of overwriting it.

Note: Use command | tee log.txt to view output live while also saving it for later.

Example: tee

bash
#!/bin/bash
echo "Important message" | tee log.txt
echo "--- contents of log.txt ---"
cat log.txt
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting -a, so tee file overwrites the file each time.
  2. Expecting tee to write errors, when it only copies standard output.
  3. Using sudo cmd > file and expecting the redirect to have root rights; use cmd | sudo tee file.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.