help-bash
[Top][All Lists]
Advanced

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

Re: Is there a way to get the output of a function without an extra bash


From: Chet Ramey
Subject: Re: Is there a way to get the output of a function without an extra bash process?
Date: Tue, 12 May 2020 10:10:43 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Thunderbird/68.8.0

On 5/11/20 5:22 PM, Peng Yu wrote:
> On 5/11/20, Chet Ramey <address@hidden> wrote:
>> On 5/10/20 6:43 PM, Peng Yu wrote:
>>> Hi Chet, Could a new feature be added to bash so that no exec is
>>> needed in the case of $(cmd). Maybe introducing a new syntax like $(:
>>> cmd)?
>>
>> I assume you mean no fork. I've thought about this a little, but there is
>> a lot of state that would have to be saved and restored to preserve the
>> subshell environment semantics of command substitution. I haven't done
>> anything to implement that.
> 
> According to Eli Schwartz, ksh supports `var=${ listcmd; }`. Sine ksh
> can do it, this features should be doable in bash? 

It depends on how much of a change to the current shell environment you're
willing to accept. If you don't care about changes to the current shell
environment, or, more precisely, are willing to accept that `listcmd' will
change the  current shell environment -- which command substitution must
not -- this is just syntactic sugar for something approximating

eval "$listcmd" > temp-file
IFS= read -r var < temp-file
rm -f temp-file
shopt -s extglob
var=${var%%+($'\n')}
shopt -u extglob        # or restore previous setting

ksh93 uses temp files for this in the general case, but you could use an
mmap-based solution if it's available.

Bash uses stdio for all its output, so a file descriptor you can use as
fd 1 is a requirement.


> Or bash and ksh are
> fundamentally different so that it is much more difficult to implement
> this in bash? Or maybe you can implement first for some restricted
> cases to make the change manageable then gradually add code for the
> more complicated cases to make this feature complete?

It's a low priority; if someone wants to take a shot at implementing it
before I get to looking at it, I'd be happy to merge it in.

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



reply via email to

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