[Top][All Lists]

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

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

From: Seth David Schoen
Subject: Re: [Help-bash] command substitution $( ) waits for child’s &
Date: Fri, 29 May 2015 16:37:05 -0700
User-agent: Mutt/1.5.21 (2010-09-15)

Patrick Schleizer writes:

> 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 '&'?
> Is it possible to prevent waiting?

Hi Patrick,

The meaning of $() is to substitute _all_ of the output of the subprocess.
The parent shell can't know that it has succeeded in collecting all of the
output until the subprocess has exited.

Imagine a different script y:

set -x
sleep 3
echo "aha, now there is output"

There is nothing in Unix IPC that lets script x distinguish between these
cases, in terms of knowing for sure whether script y will _eventually_
give more output or not, until script y has exited.  (After all, processes
that are running in the background are still allowed to write output on
stdout; they aren't necessarily promising to remain quiet.)

Seth David Schoen <address@hidden>      |  No haiku patents        |  means I've no incentive to
  FD9A6AA28193A9F03D4BF4ADC11B36DC9C7DD150  |        -- Don Marti

reply via email to

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