I am trying to use multiple process substitutions in a BASH command but I seem to be misunderstanding the order in which they resolve and redirect to each other.
The System
Ubuntu 18.04
BASH version - GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)
The Problem
I am trying to redirect an output of a command into tee
, have that redirect into ts
(adding a timestamp) and then have that redirect into split
(splitting the output into separate files). I can get the output to redirect into tee
and ts
but when redirecting into split
I run into a problem.
My Attempts
command >(tee -a >(ts '[%Y-%m-%d %H:%M:%S]' > tempfile.txt))
- this will redirect the output into process substitution of tee
then redirext to process substitution ts
and add the timestamp then redirect to tempfile.txt this is what I would expect
command >(tee -a >(ts '[%Y-%m-%d %H:%M:%S]' >(split -d -b 10 -)))
- this does nothing even though I would hope that the result would have been a bunch of 10 byte files with timestamps on the different rows.
To continue testing I tried with apparently this irrelevant because of new result I got - see edit at the bottomecho
instead to see what happens
command >(tee -a >(ts '[%Y-%m-%d %H:%M:%S]' >(echo)))
- the print from the initial tee
prints (as it should) but the echo
prints an empty line
command >(tee -a >(ts '[%Y-%m-%d %H:%M:%S]') >(split -d -b 10 -))
- This prints the command with the timestamp (as tee
and ts
should) and in addition creates 10 byte files with the command output (no timestamp on them). - this is what I expected and makes sense as the tee gets redirected to both process substitutions separately, it was mostly a sanity check
What I think is happening
From what I can tell >(ts '[%Y-%m-%d %H:%M:%S]' >(split -d -b 10 -))
are resolving first as a complete and separate command of its own. Thus split
(and echo
) are receiving an empty output from ts
which has no output on its own. Only after this does the actual command resolve and send its output to its substitution tee
.
This doesn't explain why command >(tee -a >(ts '[%Y-%m-%d %H:%M:%S]' > tempfile.txt))
does work as by this theory tee
by itself has no output so ts
should be receiving not input and should also output a blank.
All this is to say I am not really sure what is happening.
What I want
Basically I just want to understand how to make command >(tee -a >(ts '[%Y-%m-%d %H:%M:%S]' >(split -d -b 10 -)))
work in the way it seems it should. I need the commands output to send itself to the process substitution tee
which will send it to the process substitution ts
and add the timestamps which will sent it to split
and split the output to several small files.
I have tried command > >(echo)
and saw the output is blank, which is not what I expected (i expected echo
to receive and then output the command output). I think I am just very much misunderstanding how process substitution works at this point.