Skip to main content
2 of 3
edited title

How to store inner output of a nested bash command (within parenthesis) into one file and outer output into another file?

Here's my command:

(time ( \
   time sleep 1; \
   time sleep 2 \
) 2>&1 | tee inner.txt ) 2>&1 | tee outer.txt

cat inner.txt shows:

real    0m1.003s
user    0m0.001s
sys 0m0.002s

real    0m2.003s
user    0m0.001s
sys 0m0.002s

and cat outer.txt shows:

real    0m1.003s
user    0m0.001s
sys 0m0.002s

real    0m2.003s
user    0m0.001s
sys 0m0.002s

real    0m3.068s
user    0m0.005s
sys 0m0.008s

How can I get just this into outer.txt?:

real    0m3.068s
user    0m0.005s
sys 0m0.008s

Even better, how can I get inner.txt, outer.txt, and both.txt, where both.txt would contain:

real    0m1.003s
user    0m0.001s
sys 0m0.002s

real    0m2.003s
user    0m0.001s
sys 0m0.002s

real    0m3.068s
user    0m0.005s
sys 0m0.008s

Note that the time cmd I'm using above is the bash built-in function time, not the /usr/bin/time command, which I learned about through my question here: How to pass optional arguments to time command

I'm on Linux Ubuntu 18.04.