0

I have a command that I run a lot which I like to direct it's output to a file. I do this like so:

runFooBar > output.txt 2>&1

Is there a way that I can write the runFooBar output to the file output.txt while still being able to see the output in standard out/error while the command is running?

I run this on Linux and OS X machines.

2 Answers 2

3

Use the "tee" command.

echo hi | tee ./file.txt

The "tee" command takes the standard input and redirects it to both the file specified and standard out. It's called 'tee' after the t-shaped junction in plumbing (piping).

2
  • Will this write and prints both std error and std out? If not, how would I do that?
    – Nosrettap
    Dec 18, 2015 at 3:16
  • Potentially you could use tee twice; just use the second tee command to pipe standard out to standard error. Dec 19, 2015 at 20:31
1

The way to redirect both standard output and standard error to the same file is:

echo Hello 2>&1 | tee some_file.txt

This will place both of them into the same file. You may perhaps wish to put them into separate files. You can achieve that a Bash feature, process substitution, as follows:

echo Hello > >(tee standard_output.txt) 2> >(tee standard_error.txt >&2)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .