with {} - execute while loop in current shell
function f { { while : ; do echo -na; done & } 1>&2 } a=$( f ); echo "returned"
-> f() will never return !!!
() execute a while loop in a subshell
function f { ( while : ; do echo -na; done & ) 1>&2 } a=$( f ); echo "returned"
-> f() will return !!!
Why? will any of them return, but not the other? I do not understand...
My analysis:
The while will fork and start its own background process due to the & ending in the while -loop line. This background process inherits the current open list fd.
As far as I understand, since the while encapsulated, it inherits the fd encapsulation list. Here is how
{ echo log; echo err 1>&2; } 1>l_file 2>e_file
works as expected, l_file will contain "log", e_file will contain "err".
So, in the case of {} 1>&2 or () 1>&2 , bash says that he should not expect stdout not to be written.
Why is it blocking the event {} 2>&1 ?
- some sort of secret evaluation procedure?
- Perhaps closing stdout would obviously help? I donβt know the syntax, although maybe
{} 1>&- 1>&2 will help?
GNU bash version 4.3.30 (1) -release (x86_64-pc-linux-gnu)
EDIT
Based on the answers so far, I have done another analysis:
11) {}
function f { { while : ; do echo -na; done & echo "after loop" } 1>&2 echo "end of function" } a=$( f ); echo "returned"
-> after loop displayed
12) ()
function f { ( while : ; do echo -na; done & echo "after loop" ) 1>&2 echo "end of function" } a=$( f ); echo "returned"
-> after loop displayed
-> returned displayed
source share