I am wondering why
ls -1 |
while read file; do
echo $file; tail -n 100 $file > >(sleep 1 && cat > $file)
done
is faster than
ls -1 |
while read file; do
echo $file; tail -n 100 $file | (sleep 1 && cat > $file)
done
?
If there are 100 files in a directory then:
- the second command takes almost 100 seconds to process
- the first command is processed almost immediately.
bash
doesn't wait for the termination of that process substitution (which could be seen as a bug). See The process substitution output is out of the order for details. – Stéphane Chazelas Mar 29 '18 at 13:45grep < <(cmd)
, while the shell doesn't wait forcmd
,grep
does as it waits for eof on its stdin. Again, see details at the link I gave in my previous comment. – Stéphane Chazelas Mar 29 '18 at 14:22bash
waits for all pipe components. All shells wait for the last (right-most) pipe components, some don't wait for the other ones. – Stéphane Chazelas Mar 29 '18 at 15:04cmd1 | cmd2
, does bash will wait forcmd1
too? – haccks Mar 29 '18 at 15:06&
(note that background/foreground only apply to interactive shells, that's terminology linked to job control in terminals). process substitution are put in background unless they're run from a subshell itself run in foreground. In any case, that's not what I'm talking about. I'm talking about the shell waiting for the termination of processes it starts before continuing with the next command (tbc) – Stéphane Chazelas Mar 29 '18 at 15:37cmd1 | cmd2; cmd3
, bash waits for the termination of cmd1 and cmd2 (the processes it started to execute them) before runningcmd3
. Incmd1 > >(cmd2); cmd3
,bash
only waits forcmd1
before runningcmd3
.cmd2
may very well continue running whilecmd3
is running. Tryecho foo | (sleep 1; cat); ps
vsecho foo > >(sleep 1; cat); ps
– Stéphane Chazelas Mar 29 '18 at 15:39ps -j
. See also the links I gave including the discussion on the bash mailing list. – Stéphane Chazelas Mar 29 '18 at 16:23info bash "Process Substitution"
. They key word is asynchronously. – Stéphane Chazelas Mar 29 '18 at 16:28&
). – Barmar Mar 30 '18 at 00:29