6

I ran the command time echo "Hello world" | tee output.txt expecting to get the full output to both terminal stdout and the output.txt file. However, the file content is not what I expect :

Expected file content:

Hello world

real 0m0.000s
user 0m0.000s
sys 0m0.000s

Actual content:

Hello world"

can anyone help ?

Ahmed Hussein
  • 211
  • 3
  • 9
  • [How can I redirect the output of 'time' to a variable or file?](https://mywiki.wooledge.org/BashFAQ/032), *call `time` in a SubShell or block* is needed. – Rick Jun 06 '22 at 10:06

1 Answers1

9

time Writes the time statistics to stderr. So we need to redirect stderr stream into stdout, then redirect it's output to tee.

(time echo "Hello world") 2>&1 | tee output.txt

Here

  • we need to take (time echo) as a single command so used braces,
  • redirected stderr to stdout,
  • piped stdout to tee.
turbulence
  • 804
  • 4
  • 16