When you write complex bash scripts in Linux, you frequently need to pipe data through various command-line tools (like grep or awk). If the pipeline is five commands long and suddenly fails at the very end, debugging the issue is a nightmare because the intermediate data only existed in virtual memory and vanished instantly. To mathematically splice into a live data stream and save a physical copy of the data at a specific midpoint without stopping the flow, you must use the tee command.
How the tee Command Works
In physical plumbing, a “T-joint” allows water to flow straight through a pipe while simultaneously redirecting a copy of the water out of a secondary spout. The Linux tee command does exactly the same thing with digital data.
When you pipe data into tee, it performs two actions simultaneously in real-time:
- It prints the exact data directly to the standard output (allowing the next command in the pipeline to consume it).
- It physically writes a perfect, 1-to-1 copy of that data directly into a permanent file on your hard drive.
Debugging Live Pipelines
Imagine you are running a massive command that scans a log file, sorts the data, and then counts the unique IP addresses.
cat access.log | sort | uniq -c
If the final output is completely wrong, you do not know if the sort command failed or if the uniq command failed. By injecting a tee command directly into the middle of the pipeline, you can capture the exact state of the data before it hits the final command.
cat access.log | sort | tee /tmp/sorted_debug.txt | uniq -c
The pipeline executes normally, but now you have a permanent file located at /tmp/sorted_debug.txt. You can open this file and physically inspect exactly what the data looked like the millisecond it exited the sort command, allowing you to instantly isolate the failure point.
Appending Data to Log Files
By default, tee completely overwrites the target file every single time the script runs. If you are building an automated system that needs to continuously append new data to a master log file over the course of a week, you must use the -a (append) flag.
echo "Script executed successfully" | tee -a /var/log/master_script.log
This command prints the success message directly to your terminal screen, while simultaneously appending the exact same text to the absolute bottom of the master log file, ensuring no historical data is ever destroyed.