bug-bash
[Top][All Lists]
Advanced

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

nofork command substitution


From: Chet Ramey
Subject: nofork command substitution
Date: Mon, 15 May 2023 13:35:08 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.11.0

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, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/



reply via email to

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