[Top][All Lists]

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

Re: [Help-bash] redirection directly to variable when running in backgro

From: Greg Wooledge
Subject: Re: [Help-bash] redirection directly to variable when running in background?
Date: Mon, 7 Oct 2013 08:09:45 -0400
User-agent: Mutt/

On Mon, Oct 07, 2013 at 02:18:20AM +0000, adrelanos wrote:
> I would like to:
> - get the exit code
> - store stdout and stderr in a variable

No problem!

result=$(myprog 2>&1) status=$?

> - use 'wait "$!"' to let bash continue processing the event loop, so a
> trap can interrupt waiting as soon as a sigint signal or so gets caught


You can't use command substitutions with a background process.  They
only work with a foreground process.  If you want to capture the output
of a background process, you must have it write to a file or a pipe,
and then have the parent process retrieve the information from there
once the background process has terminated.

> - not redirect to temporary files, redirect directly to a variable, if
> that is possible

That is not possible.  A named pipe is the closest you can get (and is
really only suitable for small amounts of data; if the output is large
I'd just go with a temporary file).

reply via email to

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