nofork command substitution

2023-05-15 Thread Chet Ramey

The latest devel branch push has the initial implementation of `nofork'
command substitution. The excerpt from the info manual describing it is
appended.

Please test it out, and find the places I missed. Thanks.

Chet

==
   There is an alternate form of command substitution:

 ${C COMMAND; }

which executes COMMAND in the current execution environment.  This means
that side effects of COMMAND take effect immediately in the current
execution environment and persist in the current environment after the
command completes (e.g., the 'exit' builtin will exit the shell).

   The character C following the open brace must be a space, tab,
newline, '(', or '|', and the close brace must be in a position where a
reserved word may appear (i.e., preceded by a command terminator such as
semicolon).  Bash allows the close brace to be joined to the remaining
characters in the word without being followed by a shell metacharacter
as a reserved word would usually require.

   This type of command substitution superficially resembles executing
an unnamed shell function: local variables are created as when a shell
function is executing, and the 'return' builtin forces COMMAND to
complete; however, the rest of the execution environment, including the
positional parameters, is shared with the caller.

   If the first character following the open brace is a '(', COMMAND is
executed in a subshell, and COMMAND must be terminated by a ')'.  This
is similar to the '(' compound command (*note Command Grouping::).  If
the first character is a '|', the construct expands to the value of the
'REPLY' shell variable after COMMAND executes, without removing any
trailing newlines, and the standard output of COMMAND remains the same
as in the calling shell.  Bash creates 'REPLY' as an initially-unset
local variable when COMMAND executes, and restores 'REPLY' to the value
it had before the command substitution after COMMAND completes, as with
any local variable.

   For example, this construct expands to '12345', and leaves the shell
variable 'X' unchanged in the current execution environment:

 ${ local X=12345 ; echo $X; }

(not declaring 'X' as local would modify its value in the current
environment, as with normal shell function execution), while this
construct does not require any output to expand to '12345':

 ${| REPLY=12345; }

and restores 'REPLY' to the value it had before the command
substitution.

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



Re: nofork command substitution

2023-05-15 Thread Oğuz İsmail Uysal

On 5/15/23 8:35 PM, Chet Ramey wrote:

Please test it out


    $ cat
    ^Z
    [1]+  Stopped cat
    $ x=${ fg;}
    foo
    foo
    <^C or ^D here>
    $ declare -p x
    declare -- x="cat"
    $

Is this intended?



Re: nofork command substitution

2023-05-15 Thread Chet Ramey

On 5/15/23 2:42 PM, Oğuz İsmail Uysal wrote:

On 5/15/23 8:35 PM, Chet Ramey wrote:

Please test it out


     $ cat
     ^Z
     [1]+  Stopped cat
     $ x=${ fg;}
     foo
     foo
     <^C or ^D here>
     $ declare -p x
     declare -- x="cat"
     $

Is this intended?


So far, yes. Everything is shared between the comsub and its caller, with a
couple of documented exceptions. So it's just like calling `fg' in the
current execution environment, but capturing the output.

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




Re: nofork command substitution

2023-05-15 Thread Oğuz İsmail Uysal

On 5/15/23 9:51 PM, Chet Ramey wrote:

Everything is shared between the comsub and its caller, with a
couple of documented exceptions. So it's just like calling `fg' in the
current execution environment, but capturing the output.

Oh, okay then. Thanks.

How about this?

    $ declare -i x
    $ y=${ :;}
    $ declare -i z
    bash: make_local_variable: no function context at current scope
    $




Re: nofork command substitution

2023-05-15 Thread Chet Ramey

On 5/15/23 2:54 PM, Oğuz İsmail Uysal wrote:

On 5/15/23 9:51 PM, Chet Ramey wrote:

Everything is shared between the comsub and its caller, with a
couple of documented exceptions. So it's just like calling `fg' in the
current execution environment, but capturing the output.

Oh, okay then. Thanks.

How about this?


Thanks, missed a restore of variable_context.

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