4

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

2 Answers 2

14

Use tee:

./child | tee file

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

Sign up to request clarification or add additional context in comments.

1 Comment

Another tip: 'tee -a' is for appending to files if you want that, equivalent to using >> instead of > with normal shell redirection.
-1

I use: sponge, from moreutils http://linux.die.net/man/1/sponge

you can do something like that:

$ grep something largefile | sponge largefile

1 Comment

How does it fullfill "Display the output of the process in real time"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.