Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix and tee — chain of commands

Tags:

shell

unix

tee

In a Unix environment, I want to use tee on a chain of commands like so:

$ echo 1; echo 2 | tee file
1
2

$ cat file
2

Why does file only end up as having the output from the final command?

For the purposes of this discussion, let's assume I can't break them apart and run the commands separately.

like image 851
Mike Avatar asked May 17 '10 18:05

Mike


People also ask

What is the tee command in UNIX?

The tee command, used with a pipe, reads standard input, then writes the output of a program to standard output and simultaneously copies it into the specified file or files. Use the tee command to view your output immediately and at the same time, store it for future use.

What is tee in command line?

The tee command is normally used to split the output of a program so that it can be both displayed and saved in a file. The command can be used to capture intermediate output before the data is altered by another command or program. The tee command reads standard input, then writes its content to standard output.

What does tee command mean in Linux?

What Does tee Command Do in Linux? The tee command reads standard input (stdin) and writes it to both standard output (stdout) and one or more files. tee is usually part of a pipeline, and any number of commands can precede or follow it.


1 Answers

It has only the output of the second command, as the semicolon indicates a new statement to the shell.

Just put them into parentheses:

(echo 1; echo 2) | tee file
like image 169
WhirlWind Avatar answered Oct 04 '22 02:10

WhirlWind