help-bash
[Top][All Lists]
Advanced

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

Re: Does this line create a subshell?


From: Greg Wooledge
Subject: Re: Does this line create a subshell?
Date: Sun, 22 May 2022 16:45:57 -0400

On Sun, May 22, 2022 at 04:19:31PM -0400, Robert E. Griffith wrote:
> Does this line create a subshell when 'foo' is a loadable builtin that
> produces output on stdout?
> 
>    local line; read -r line <(builtin foo)

Yes.  <() always creates a (background) subshell process.  If the command
inside <() is a builtin, then the subshell will simply run it.  If it's
an external program, the subshell may exec it (if the shell's optimization
deems that to be possible), or else fork-and-exec it.

> Would that line be about the same in the amount of runtime work done (i.e.
> just as quick) as writing the builtin to set the value of a shell variable
> whose name is passed into it?
> 
>    local line; builtin foo "line"

Oh, is this a *custom* builtin that you're writing?  If at all possible,
writing the custom builtin to store its results in a shell variable is
the most efficient thing you can do.

Second most efficient would be writing the result to a temp file and
then reading it from the temp file.  But there's a catch here: if you
have to fork mktemp(1) or an equivalent program to create the temp file,
and then fork rm(1) to get rid of it, this is no longer quite as
efficient.

Third most efficient would be either  var=$(mybuiltin)  or what you had
originally:  read -r var < <(mybuiltin)  .  Or, this may be the second
most efficient choice, depending on how you have to deal with temp files.



reply via email to

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