help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] command substitution $( ) waits for child’s &


From: Chet Ramey
Subject: Re: [Help-bash] command substitution $( ) waits for child’s &
Date: Fri, 29 May 2015 20:48:21 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:31.0) Gecko/20100101 Thunderbird/31.6.0

On 5/29/15 7:16 PM, Patrick Schleizer wrote:
> Hi!
> 
> script x:
> #!/bin/bash
> set -x
> output="$(./y 2>&1)"
> 
> script y:
> #!/bin/bash
> set -x
> sleep 3 &
> 
> Script x waits until script y exits.
> 
> Due to using command substitution $( ). Without $( ) it wouldn't wait.
> 
> Why does command substitution ignore the '&'?

Bash doesn't wait for the command substitution process.  It reads from the
pipe until it gets EOF.  A pipe open for reading won't return EOF until all
the processes that have it open for writing have exited.

> Is it possible to prevent waiting?

Change the sleep line to close the file descriptors open on the pipe:

sleep 3 >&- 2>&- &

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]