Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unix shell, redirect output but keep on stdin

I'd like to have a shell script redirect stdout of a child process in the following manner

  1. Redirect stdout to a file
  2. Display the output of the process in real time

I know I could do something like

#!/bin/sh

./child > file
cat file

But that would not display stdout in real time. For instance, if the child was

#!/bin/sh

echo 1
sleep 1
echo 2

The user would see "1" and "2" printed at the same time

like image 402
Mike Avatar asked Dec 13 '22 21:12

Mike


1 Answers

Use tee:

./child | tee file

tee will copy its standard input to any file on the command line and to standard output as well.

like image 118
R Samuel Klatchko Avatar answered Jan 02 '23 08:01

R Samuel Klatchko