Basically I am trying to understand the difference between those commands:
cat <<< yolo | tee f.txt
echo yolo | tee t.txt
And those commands:
cat <<< yolo > >(tee f.txt)
echo yolo > >(tee t.txt)
The first two commands have the exact same result: "yolo" is printed and the terminal gives back the control after that, which is exactly what I would expect.
[user@localhost ~]$ cat <<< yolo | tee f.txt
yolo
[user@localhost ~]$ echo yolo | tee t.txt
yolo
But with process substitution, something strange occurs with echo.
[user@localhost ~]$ cat <<< yolo > >(tee f.txt)
yolo
[user@localhost ~]$ echo yolo > >(tee t.txt)
[user@localhost ~]$ yolo
The terminal gives back the control before the text is printed out. Why do I get the control sooner in this case?
This must have something to do with how processes are opened and how file descriptors are passed between processes, but I am kind of reaching the limits of my knowledge.
If I pipe it to anything else, everything goes back to normal, e.g. echo yolo > >(tee t.txt) | cat.
What’s even more strange is that xargs'ing into echo works well:
[user@localhost ~]$ xargs echo <<< yolo > >(tee t.txt)
yolo
But you could say that xargs is the main program here, not echo.
And if I use an input process substitution with cat I have mixed results:
cat < <(echo yolo) > >(tee t.txt)
Sometimes it gives me this:
[user@localhost ~]$ cat < <(echo yolo) > >(tee t.txt)
[user@localhost ~]$ yolo
And sometimes this:
[user@localhost ~]$ cat < <(echo yolo) > >(tee t.txt)
yolo
So I guess this may have something to do with how fast the system executes the command, which kind of makes it unpredictable.
Does that mean that the output process substitution (e.g. tee in this example) runs in the background?