if i want to display "aaa" on screen:
(1)$: echo aaa | cat ... works OK
(2)$: echo aaa | ( cat ) ... works OK
(3)$: echo aaa | ( cat & ) ... NOT working
(4)$: ( echo aaa & ) | cat ... works OK
(5)$: echo aaa | ( cat <&0 & ) ... works ok in BASH (but not in SH)
(6)$: echo aaa | ( cat <&3 & ) 3<&0 ... works ok in BASH and SH
conlusion from (3) and (4) -> detached process still have connected output that can be controlled, used, redirected…, but not input!
My question is: does someone understand why and how line (5) works ???
… "<&0" is short for "0<&0", why redirecting 0 to 0 is solution, and what really happens behind with input of detached process. Subshell's are not the problem, using braces {…} instead of (…) provide same results.
… and question2: is there better solution for "giving input to detached process" than line (6).
Best Answer
Yes, as required by POSIX, commands started in background with
&
have their standard input redirected from/dev/null
.And indeed
is the most obvious way to work around it.
It's not clear why you'd want to run part of pipeline in background though.