0
0
Linux CLIscripting~10 mins

tee for splitting output in Linux CLI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - tee for splitting output
Run command producing output
Output stream sent to tee
Write to file
End of process
The command output is sent to tee, which splits it to both a file and the screen simultaneously.
Execution Sample
Linux CLI
echo "Hello World" | tee output.txt
This command prints 'Hello World' on the screen and saves it to output.txt at the same time.
Execution Table
StepActionInputOutput to ScreenOutput to File
1Run echo commandNo inputHello World
2Pipe output to teeHello WorldHello WorldHello World
3tee writes to fileHello WorldHello WorldHello World
4tee outputs to screenHello WorldHello WorldHello World
5Process endsNo inputHello World displayedFile saved
💡 Command finishes after tee writes output to both screen and file.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Output StreamemptyHello WorldHello WorldHello WorldHello World
File output.txtemptyemptyHello WorldHello WorldHello World
Screen outputemptyHello WorldHello WorldHello WorldHello World
Key Moments - 2 Insights
Why do we see the output on the screen even though it is saved to a file?
Because tee duplicates the output stream: it sends the output both to the file and to the screen as shown in execution_table steps 2 to 4.
What happens if the file already exists?
tee overwrites the file by default, replacing its contents with the new output, as implied in step 3 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output to the screen at step 3?
ANo output
BHello World
CEmpty line
DError message
💡 Hint
Check the 'Output to Screen' column at step 3 in the execution_table.
At which step does tee write the output to the file?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Output to File' column in the execution_table.
If we remove tee from the command, what changes in the variable_tracker?
AOutput to file is empty
BOutput to file remains the same
COutput to screen disappears
DOutput stream becomes empty
💡 Hint
Without tee, output is not saved to file, check 'File output.txt' row in variable_tracker.
Concept Snapshot
tee command duplicates output streams.
Syntax: command | tee filename
Outputs go to both screen and file.
Overwrites file by default.
Use -a to append instead of overwrite.
Full Transcript
The tee command in Linux takes the output of a command and splits it so that it goes to both the screen and a file. For example, echo "Hello World" | tee output.txt prints Hello World on the screen and saves it to output.txt at the same time. The flow starts with the command producing output, which is piped to tee. Tee then writes the output to the file and also sends it to the screen. This way, you can see the output live and keep a copy saved. If the file exists, tee overwrites it unless you use the -a option to append. This is useful when you want to log output but still watch it as it happens.