Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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).

share|improve this question
up vote 7 down vote accepted

Yes, as required by POSIX, commands started in background with & have their standard input redirected from /dev/null.

And indeed

{ cmd <&3 3<&- & } 3<&0

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.

share|improve this answer
reason was to get PID of one specific command inside pipe chain ( cmd1 | { cmd2 &; pid2=$!; } | cmd3 ... , so /dev/null==&0 is standard for BG proces, do you also know why "0<&0" works in bash ... and is it safe? – Asain Kujovic Apr 4 '13 at 17:50
@OmerMerdan POSIX says In all cases, explicit redirection of standard input shall override this activity, which contradicts somehow what it said above, so I believe bash interprets it (and that sounds like a reasonable interpretation) as cancelling the /dev/null redirection. Now, not many shells do the same. Ash and pdksh don't. – Stéphane Chazelas Apr 4 '13 at 17:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.